MVC5中基于令牌的授权实现

时间:2014-05-23 10:00:07

标签: asp.net-web-api oauth

让我先用角色扮演舞台:

  1. MVC 5应用程序
  2. WebAPI
  3. 我需要实现基于令牌的安全性来从#1访问#2。

    我已经拥有的东西:

    1. 在#2

      中创建了一个启动类
      public void Configuration(IAppBuilder app)
      {
          // token generation
          app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
          {
      
             AllowInsecureHttp = true,
      
             TokenEndpointPath = new PathString("/token"),
             AccessTokenExpireTimeSpan = TimeSpan.FromHours(8),
      
             Provider = new MyServerProvider()
          });
      
          // token consumption
          app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
          var httpConfiguration = new HttpConfiguration();
          WebApiConfig.Register(new HttpConfiguration());
      
          app.UseWebApi(httpConfiguration);
      }
      
    2. 互联网上随处可见的标准代码。

      1. 这是MyServerProvider中的代码也在#2

        public class MyServerProvider: OAuthAuthorizationServerProvider
        {
            public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
            {
                context.Validated();
                await Task.FromResult(0);
            }
        
            public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
                if (context.UserName == "one" && context.Password == "two")
                {
                    var id = new ClaimsIdentity(context.Options.AuthenticationType);
                    id.AddClaim(new Claim("name", context.UserName));
                    id.AddClaim(new Claim("role", "user"));
        
                    context.Validated(id);
                }
                else
                {
                    context.Rejected();
                }
        
                await Task.FromResult(0);
            }
        }
        
      2. 另一个在#3

        中提供令牌的类
        public class TokenProvider
        {
            public TokenResponse _tokenValue { get; set; }
            public  string _accessToken { get; set; }
        
            public string GetToken(string tokenEndpoint, string userName, string password)
            {
                var client = new OAuth2Client(new Uri(tokenEndpoint));
                var tokenResponse = client.RequestResourceOwnerPasswordAsync(userName, userName).Result;
        
                _tokenValue = tokenResponse;
                _accessToken = _tokenValue.AccessToken;
        
                return _accessToken;
            }
        }
        
      3. 到目前为止一切顺利。

        Q1。现在当来自控制器的请求命中api或api时     称为表单JavaScript,会发生什么? Q2。从哪个方法来     上面叫做?

        Q3。 GrantResourceOwnerCredentials做了什么?

        Q4。上述问题中的上下文对象有什么以及如何进行     是否会为其添加userName和Password以及声明如何存储在Cookie中?

        Q5。如果我必须将令牌存储在cookie中并将其用于后续请求,我是否应该写     #1中控制器的OnActionExecuting方法中的代码?

        这听起来非常具体,但事实并非如此。我试图从现实世界的场景中理解基于令牌的身份验证,我是新手。

        我已经浏览了ThinkTecture Github回购中的样本,他们都在解释它们方面做得很好但是我坚持实施它。

        非常感谢任何帮助。

        问候。

0 个答案:

没有答案
相关问题