【问题标题】:Auth0 - Authenticating with RS256 using JWT on Owin with bearer-accessTokenAuth0 - 在 Owin 上使用带有承载访问令牌的 JWT 使用 RS256 进行身份验证
【发布时间】:2018-11-25 08:27:41
【问题描述】:

在使用普通嵌入式登录实现 Auth0 身份验证/授权时,我能够对用户进行身份验证并取回有效的 accessToken/idToken

  • 初始化

    webAuth = new auth0.WebAuth({
        domain: 'xxx.auth0.com',
        clientID: 'myclientid',
        responseType: 'token id_token'
    });
    
  • 成功获取token。

    webAuth.client.login({
        realm: _Connection,
        username: 'aaa@b.com',
        password: 'password',
        audience: 'https://xxx.auth0.com/api/v2/',
        scope: 'openid profile email'
    }, function (err, args) {
        if (!err) 
          {
           webAuth.client.userInfo(token, function (args, authUserData) {
            var ajaxAdapter = breeze.config.getAdapterInstance("ajax");
            ***Setting bearer token to Global level.**
            ajaxAdapter.defaultSettings = {
             headers: ({ "Authorization": "Bearer " + token })
            };
            myAPICall(args.email).then({}).fail({});
           });
        }
    });
    
  • 使用 OWIN 验证 RS256 签名 JWT 的服务器代码。

    private void ConfigureAuthZero(IAppBuilder app)
    {
        var issuer = $"https://{ConfigurationManager.AppSettings["Auth0:Domain"]}/";
        var audience = ConfigurationManager.AppSettings["Auth0:ClientID"];
        var apiIdentifier = ConfigurationManager.AppSettings["Auth0:ApiIdentifier"];
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        string certificatePath = HostingEnvironment.MapPath("~/mycertificate.cer");
        var certificate = new X509Certificate2(certificatePath);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidAudience = audience,
                    ValidIssuer = issuer,
                    IssuerSigningKeyResolver = (token, securityToken, identifier, parameters) => new X509SecurityKey(certificate)
                }
            });
    }
    

我的问题

上面的服务器代码不会授权用户。 但是,如果我将ValidAudience = "https://xxx.auth0.com/api/v2/" 设置为 Auth0 API Identifier,则 API 方法成功授权(状态 200)用户。

但这一次它不会给 ClaimsIdentity.ClaimsClaimTypes.Email

我在这里错过了什么?

【问题讨论】:

    标签: asp.net-web-api2 owin auth0


    【解决方案1】:

    我的错误:

    1. 我应该将 ApiIdentifier 传递给 ValidAudience 值。
    2. 当我在授权用户时传递 accessToken 时,由 accessToken 声明不包含 ClaimTypes.Email 的时间,所以 我需要将 Auth0 中的规则设置为:How to set the rules in Auth0。 稍后我可以将我的服务器 api 逻辑检查为(下面的代码) 验证用户。
      (User.Identity as ClaimsIdentity)?.Claims.FirstOrDefault(c => c.Type == "you-have-set-this-rule-in-auth0")?.Value;

    只是为了附加组件,Link 在实现 Auth0 时值得一读。 Auth0 提供了一个不错的 nuget 包 Auth0.OpenIdConnectSigningKeyResolver,它在上面提供的链接中有很好的用途。

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 1970-01-01
      • 2017-01-22
      • 2014-02-11
      • 2023-03-20
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2017-02-04
      相关资源
      最近更新 更多