【问题标题】:Jwt with new clientJwt 与新客户
【发布时间】:2017-10-02 05:43:58
【问题描述】:

我已经实现了 JWT 身份验证

我在启动文件中有类似功能

public void ConfigureOAuth(IAppBuilder app)
        {
            var issuer = "http://jwtauthzsrv.azurewebsites.net";
            //var audience = "fd9eeeb25b694ff1bfd2260cf0d665c3";
            //var secret = TextEncodings.Base64Url.Decode("ixioSD5UPMMfGFUvxfblU3elZAnUvtoleVI0qu_7SrI");
            List<string> secret = new List<string>();
            List<string> audience = new List<string>();

            secret.Add(Convert.ToBase64String(TextEncodings.Base64Url.Decode("adFu3IhHjNibtVzy7Uon7l9CQE97XQ8YOKnLzLWlb1s")));
            //secret.Add(Convert.ToBase64String(TextEncodings.Base64Url.Decode("YqnwwJFI6HTQheAGvHI9ycuhgRmkpJDgOQDkDnahG2I")));
            // secretkeys.Add(TextEncodings.Base64Url.Decode("YqnwwJFI6HTQheAGvHI9ycuhgRmkpJDgOQDkDnahG2I").ToString());
            audience.Add("746b0e9e0bb44bfda086bc7f03f427c5");
           // audience.Add("7ef27cd3397d41e4848c3a85cca9e737");





            // Api controllers with an [Authorize] attribute will be validated with JWT
            app.UseJwtBearerAuthentication(
                new JwtBearerAuthenticationOptions
                {
                    AuthenticationMode = AuthenticationMode.Active,
                    AllowedAudiences =  audience.AsEnumerable(),//new[] { audience },
                    //AllowedAudiences = new[] { audience },
                    IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                    {
                        new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
                        //new SymmetricKeyIssuerSecurityTokenProvider(issuer,secret.AsEnumerable())
                    },
                    Provider = new OAuthBearerAuthenticationProvider
                    {
                        OnValidateIdentity = context =>
                        {
                            context.Ticket.Identity.AddClaim(new System.Security.Claims.Claim("newCustomClaim", "newValue"));
                            return Task.FromResult<object>(null);
                        }
                    }
                });

        }

我假设每当调用具有 authorize 属性的 api 时,它应该调用 ConfigureOAuth 以便它应该从数据库加载所有 cient id 并通过 jwt 验证,但是在调试时我看到启动文件运行只有当我运行应用程序时,

我怎样才能让它在调用任何 api 时调用 ConfigureOAuth 这个函数,以便它也应该加载新的 clientid 并进行验证。

更新

app.UseJwtBearerAuthentication(
                new JwtBearerAuthenticationOptions
                {
                    AuthenticationMode = AuthenticationMode.Active,
                    AllowedAudiences =  select from database),

上述行仅在我发布和部署项目时初始化一次,而不是在进行新的 api 调用时。

如果创建了新用户,则会出现问题,新用户的 id 不会进入允许的受众

谢谢

【问题讨论】:

  • 状态如何?我的(更新的)答案对您有帮助吗?

标签: c# asp.net-mvc oauth owin startup


【解决方案1】:

ConfigureOAuth 应该只在启动时运行一次,所以行为是正确的。

使用app.UseJwtBearerAuthentication,您可以向请求管道添加一个用于身份验证的中间件。您已经将允许的受众添加到配置中,因此在处理 [Authorize] 装饰的 api 期间不会再次加载它。然后中间件将检查请求中是否存在授权标头,并检查提供的 JWT 是否有效以及受众是否与配置的受众匹配。

根据您的更新进行更新:

如果创建了新用户,则会出现问题,新用户的 id 不会进入允许的受众

“Audiences”不是用户,而是可以使用令牌的资源服务器,另见此处:JWT (Json Web Token) Audience "aud" versus Client_Id - What's the difference?

当您处理一个用[Authorize] 装饰的请求时,用户身份验证不会发生,但当用户在令牌端点请求令牌时已经更早了。 JWT auth 中间件将仅检查它在请求的 Authorization 标头中找到的令牌是否具有与 AllowedAudiences 匹配的 aud 值。但是此时,用户已经通过了身份验证。

您访问用户数据库的位置将在不同的类中,例如

public class MyAuthProvider : OAuthAuthorizationServerProvider
{
    public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
    if(Authenticate(context.UserName, context.Password))
        ...
    }
}

这就是您验证用户、向 JWT 添加声明并最终创建 JWT 令牌的地方。

我推荐阅读 Taiseer Joudeh 的博客:http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/

【讨论】:

  • 例如,我给出了硬编码的值,但那些是从数据库加载受众。
  • 从哪里获取受众并不重要,app.UseJwtBearerAuthentication( new JwtBearerAuthenticationOptions { AllowedAudiences = 告诉中间件允许哪些受众,JWT 将根据这些配置的受众进行检查。
  • 但仍然只是一次性配置,调用的实际处理由中间件完成
  • 如果我做 app.UseJwtBearerAuthentication( new JwtBearerAuthenticationOptions { AllowedAudiences = list 所以新列表将绑定在每个具有授权属性的api调用中
猜你喜欢
  • 2017-10-02
  • 2021-06-21
  • 1970-01-01
  • 2019-10-11
  • 2015-09-29
  • 1970-01-01
  • 2018-04-27
  • 2021-10-26
相关资源
最近更新 更多