【发布时间】:2019-01-18 20:39:19
【问题描述】:
我正在使用 ASP.NET Core 2.1 和 Auth0。
当我尝试检索访问令牌以访问我自己的 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);
}
};
});
【问题讨论】:
-
查看
options.TokenValidationParameters设置多个受众。 -
但问题是我不想要多个观众。我只需要我自己的。
-
您需要其他受众来检索用户的用户信息。
-
我想我明白了。添加的受众是 auth0 管理 API?这意味着我需要配置多个受众?
-
是的,它是 Auth0 添加的,用于授权客户端访问 Auth0 的 userinfo 端点。是的,您不应该使包含比您需要的更多受众的令牌无效。只需检查您需要的那些并忽略其余部分。
标签: c# asp.net-core auth0