【问题标题】:Web API 2 OWIN Bearer token custom authenticationWeb API 2 OWIN Bearer 令牌自定义身份验证
【发布时间】:2014-07-06 21:30:21
【问题描述】:

我想实现自己的自定义 WebApi 身份验证。我尝试在 ApplicationOAuthProvider 中修改 GrantResourceOwnerCredentials 方法。

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        using (UserManager<IdentityUser> userManager = _userManagerFactory())
        {
            IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);

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

            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
    }

我替换认证:

IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);

我的 Web 服务调用返回 true 表示已通过身份验证,返回 false 表示未通过身份验证。

if (authenticated)
{
IdentityUser user = new IdentityUser("username");
}

但我不知道如何处理 ClaimsIdentity。谁有我可以参考的样品?谢谢。

【问题讨论】:

    标签: c# authentication asp.net-web-api owin


    【解决方案1】:

    晚了,但不妨记录一下,因为它仍然相关。如果您的意思是如何分配和解决票证中的索赔,您可以选择。您可以实现 IUserClaimsStore (documentation for said object),或者简单地创建自己的声明存储服务。最手动的方法是在验证用户登录并检索其身份后明确地将声明分配给用户。这是一个使用您的代码的简单示例:

    using (UserManager<IdentityUser> userManager = _userManagerFactory())
        {
            IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);
    
            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
    
            user.AddClaim(new Claim("sub","whateveryouwantthesubjectnametobe"));
            user.AddClaim(new Claim("someotherclaim","whosecontentsyoudeterminehoweveryouwouldlike"));
    
            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
    

    应该是这样,声明将成为您的令牌的一部分,并且它应该在下一个请求时自动填充 ClaimsPrincipal。如果要查看请求中票证的反序列化,创建一个简单的 OAuthBearerAuthenticationProvider 实现,并覆盖 ValidateIdentity 方法:

    private class SuperSecretBearerAuthClass: OAuthBearerAuthenticationProvider
        {
            public override Task ValidateIdentity(OAuthValidateIdentityContext context)
            {
                var claims = context.Ticket.Identity.Claims; //examine claims here
                base.ValidateIdentity(context);
                return Task.FromResult<object>(null);
            }
        }
    

    startup.cs/无论你在哪里配置你的身份验证都会如下所示:

     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
     {
           Provider = new SuperSecretBearerAuthClass()
    
      });
    

    【讨论】:

      【解决方案2】:

      我不太明白您在寻找什么,但您可能想看看 OAuth 嵌入式服务器的 Thinktectures 实现。

      查看他们的 Thinktecture.IdentityModel 项目和示例 - https://github.com/thinktecture/Thinktecture.IdentityModel

      他们有一个名为 samples\OAuth2\EmbeddedAuthorizationServer 的示例项目,该项目使用 GrantResourceOwnerCredentials 方法进行身份验证。

      这里有一篇关于 ClaimsIdentity 的简单博客文章,可能也会有所帮助。 http://www.remondo.net/simple-claims-based-identity/

      【讨论】:

        猜你喜欢
        • 2013-11-25
        • 2016-04-05
        • 1970-01-01
        • 2014-10-05
        • 2020-01-14
        • 2014-11-18
        • 2016-12-31
        • 2018-06-03
        • 1970-01-01
        相关资源
        最近更新 更多