【发布时间】: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.Claims 和 ClaimTypes.Email
我在这里错过了什么?
【问题讨论】:
标签: asp.net-web-api2 owin auth0