2016-05-20 67 views
0

参考此decision template可以为我的应用程序选择正确的授权类型。 enter image description here 我不太清楚,使用哪种授权类型。我应该选择哪种OAuth2.0授权

该方案是如下:

  1. 资源服务器,的WebAPI 2.0(域A)
  2. 授权服务器(域B)
  3. ASP.NET MVC应用,使得基于jQuery Ajax请求到资源服务器

基本上我想要的是,我的MVC应用程序的所有loggedIn用户可以立即使用持票人从客户端浏览器直接向资源服务器发出ajax请求验证。

访问令牌所有者将是浏览器用户代理,它将在本地存储器中存储访问令牌,例如,对吗?每个登录用户都将拥有自己的身份验证令牌。 MVC应用程序的客户端(浏览器)是“第一方”客户端,对不对?

它会在最后使用什么授权类型 - >密码授权?

回答

1

您的密码的grant_type将是“密码”它自己。例如,从我的应用程序中拿这个例子:

JAVASCRIPT - 使用角度与OAuth2.0和c#WebAPI 2.0(MVC)的Auth服务 - 使用的模式。 - API

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 
      var source = LogUtility.InitializeContext(this.GetType()); 

      try 
      { 
       var allowedOrigin = context.OwinContext.Get<String>("clientAllowedOrigin") ?? "*"; 

       context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); 

       var signOnContext = await new AccessControlBusinessLogicLayer().LoadUserSignOnContextOrDefaultByCredentials(context.Options.AuthenticationType, context.UserName, context.Password); 

       if (signOnContext == null) 
       { 
        context.SetError("invalid_grant", "The user name or password is incorrect."); 

        return; 
       } 

       context.OwinContext.Set<String>("userSecurityStamp", signOnContext.SecurityStamp); 

       var authenticationProperties = new AuthenticationProperties(
        new Dictionary<String, String> 
        { 
         { "client_id", context.ClientId } 
        }); 
       var authenticationTicket = new AuthenticationTicket(signOnContext.Identity, authenticationProperties); 

       if (context.Scope[0].ToLower() == "website") 
       { 
        using (var userContext = new AccessControlContext()) 
        { 
         using (var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(
          var user = AccessBL.LoadUserOrDefaultByUserName(context.UserName); 

          var userId = user.Id; 
         } 
        } 
       } 

       context.Validated(authenticationTicket); 
      } 
      catch (Exception exception) 
      { 
       throw exception; 
      } 
     } 

这将检查授权并为当前上下文

C#:

return $http({ 
       url: $rootScope.globals.apiPath + "/someController/token", 
       method: 'POST', 
       data: "userName=" + encodeURIComponent(username) + 
         "&password=" + encodeURIComponent(password) + 
         "&Scope=" + "website" + 
        "&grant_type=password" + // here I pass the type as password to the web api 
        "&client_id=clientIdExample", 
       headers: { 'Content-Type': 'application/x-www-form-urlencoded' } 
      }); 

这是然后在API使用授权的资源所有者凭证像这样处理当前用户。希望这有助于解决模糊问题。这意味着密码授予将是来自Web客户端的授予资源,而不是隐含的。

+0

嗯,我宁愿在mvc应用程序登录过程(用户名+密码)期间请求承载认证令牌,然后将认证令牌(承载)发送回浏览器客户端。然后,持票人令牌随后通过使用jQuery Ajax的Authorization Header在每个请求上发送给承载安全的WebApi。当使用密码授权时,这将是我想象的过程。对?因为我不想将用户凭据传递给浏览器,然后传递给资源服务器。 – Legends

+0

专注于这一点正是您需要做的,您只需通过请求的标头将承载发送到安全的web api,并且您绝对不希望将用户凭证来回传递给浏览器,这会使其承受安全风险。所以从我在流程图中可以看到的情况来看,如果您使用“密码”作为授予类型,那么您应该朝着正确的方向前进。而且我也对此进行了大量研究,并提出了使用此解决方案的最佳实践:http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api -2-owin-asp-net-identity/ – ThatAwesomeCoder

+0

但在你的例子中,你使用凭证通过angular.js在浏览器中运行来调用令牌服务端点...?! – Legends

相关问题