Auth0 - 在 Owin 上使用带有 bearer-accessToken 的 JWT 通过 RS256 进行身份验证
Auth0 - Authenticating with RS256 using JWT on Owin with bearer-accessToken
在使用普通的嵌入式登录实施 Auth0 Authentication/Authorization 时,我能够对用户进行身份验证并取回有效的 accessToken/idToken.
初始化
webAuth = new auth0.WebAuth({
domain: 'xxx.auth0.com',
clientID: 'myclientid',
responseType: 'token id_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 标识符,那么 API 方法成功授权(状态 200)用户。
但是这次不会给ClaimsIdentity.Claims和ClaimTypes.Email
我在这里错过了什么?
我的错误:
- 我应该将 ApiIdentifier 传递给 ValidAudience 值。
- 当我在授权用户时传递 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,它在上面提供的 link.
中有很好的用途
在使用普通的嵌入式登录实施 Auth0 Authentication/Authorization 时,我能够对用户进行身份验证并取回有效的 accessToken/idToken.
初始化
webAuth = new auth0.WebAuth({ domain: 'xxx.auth0.com', clientID: 'myclientid', responseType: 'token id_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 标识符,那么 API 方法成功授权(状态 200)用户。
但是这次不会给ClaimsIdentity.Claims和ClaimTypes.Email
我在这里错过了什么?
我的错误:
- 我应该将 ApiIdentifier 传递给 ValidAudience 值。
- 当我在授权用户时传递 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,它在上面提供的 link.
中有很好的用途