[英]GetTokenAsync returns 2 audiences in ASP.NET Core 2.1 using auth0
我正在使用ASP.NET Core 2.1和Auth0。
当我尝试检索acces_token来访问自己的API时,我使用了
string accessToken = await HttpContext.GetTokenAsync("access_token");
奇怪的是,当我将令牌粘贴到https://jwt.io/上时,它表明已添加了一个观众。 事实是不允许两个访问者,因此令牌无效。 添加的受众群体以/ userinfo结尾
有人可以解释为什么我的访问凭证中有两个观众吗?
我在ConfigureServices中使用以下代码
// Add authentication services
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("Auth0", options =>
{
// Set the authority to your Auth0 domain
options.Authority = $"https://{Configuration["Auth0:Domain"]}";
// Configure the Auth0 Client ID and Client Secret
options.ClientId = Configuration["Auth0:ClientId"];
options.ClientSecret = Configuration["Auth0:ClientSecret"];
// Set response type to code
options.ResponseType = "code";
// Configure the scope
options.Scope.Clear();
options.Scope.Add("openid");
// Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0
// Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard
options.CallbackPath = new PathString("/signin-auth0");
// Configure the Claims Issuer to be Auth0
options.ClaimsIssuer = "Auth0";
// Saves tokens to the AuthenticationProperties
options.SaveTokens = true;
options.Events = new OpenIdConnectEvents
{
// handle the logout redirection
OnRedirectToIdentityProviderForSignOut = (context) =>
{
var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";
var postLogoutUri = context.Properties.RedirectUri;
if (!string.IsNullOrEmpty(postLogoutUri))
{
if (postLogoutUri.StartsWith("/"))
{
// transform to absolute
var request = context.Request;
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
}
logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}";
}
context.Response.Redirect(logoutUri);
context.HandleResponse();
return Task.CompletedTask;
},
OnRedirectToIdentityProvider = context =>
{
context.ProtocolMessage.SetParameter("audience", "MY_OWN_AUDIENCE_URL");
return Task.FromResult(0);
}
};
});
有人可以解释为什么我的访问凭证中有两个观众吗?
第二个受众是userinfo端点。 userinfo端点是OpenID Connect协议的一部分 ; 它公开最终用户的配置文件信息,并且由于openid
范围而存在。
Auth0收到授权请求后,将检查请求的audience
和scope
参数。 如果audience
是自定义API,并且scope
包括openid
,则access_token
将包括两个访问者:一个用于您的自定义API,另一个用于Auth0 userinfo端点。
这是来自https://auth0.com/docs/tokens/access-token的支持性报价
当将受众设置为自定义API且scope参数包含
openid
值时,则生成的访问令牌将是JWT,它对于检索用户的个人资料和访问自定义API均有效。 在aud
此JWT的要求将包括两个值:YOUR_AUTH0_DOMAIN/userinfo
和自定义API的唯一标识符。
工作
我将它与Startup类中放置在ConfigureServices中的下一个代码一起使用。 在“配置列表”中,我放置了来自Auth0 userinfo API和我自己的API的受众。
// Multiple audiences
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidAudiences = Configuration.GetSection("Auth0:Audiences").Get<List<string>>(),
ValidateLifetime = true
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.