c# - 从 IProfileService 设置的其他声明在 MVC 客户端的 OpenIdConnect 处理程序中不可用
问题描述
我正在使用在 .NET Core 上运行的 Identity Server 4 和 .NET Framework v4.6.2 MVC 应用程序。我使用配置文件服务从身份服务器设置附加声明:
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
if (context.Caller.Equals("ClaimsProviderAccessToken") || context.Caller.Equals("ClaimsProviderIdentityToken"))
{
foreach (var group in groups)
{
// Custom logic to add additional claims.
context.IssuedClaims.Add(new Claim(ClaimTypes.Role, groupName));
}
}
}
public Task IsActiveAsync(IsActiveContext context)
{
return Task.CompletedTask;
}
当我尝试使用 .NET Core MVC 客户端时,客户端可以使用此处设置的附加声明。但是,对于在 ASP.NET Framework 中运行的 MVC 客户端,这些声明在context.AuthenticationTicket.Identity.Claims
. 但是当我检查访问令牌时,声明就在那里context.ProtocolMessage.AccessToken
。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
ExpireTimeSpan = new TimeSpan(0, Configuration.SessionTimeoutInMinutes, 0),
SlidingExpiration = true,
CookieSameSite = Microsoft.Owin.SameSiteMode.None,
CookieSecure = CookieSecureOption.Always
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
ResponseType = "id_token token",
Scope = "openid profile roles api",
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
// The claims are not available here.
foreach (var claim in context.AuthenticationTicket.Identity.Claims.Where(x => x.Type == JwtClaimTypes.Role).ToList())
{
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Role, claim.Value));
}
// But, the claims are available in the access token.
context.Response.Cookies.Append("access-token", context.ProtocolMessage.AccessToken, new Microsoft.Owin.CookieOptions() { SameSite = Microsoft.Owin.SameSiteMode.None, Secure = true });
return Task.FromResult(0);
},
}
});
这里出了什么问题?如果我需要发布更多代码,请告诉我。
解决方案
AlwaysIncludeUserClaimsInIdToken = true
在 Identity Server 中注册 MVC 客户端时使用。
推荐阅读
- javascript - Vue.JS 2.5.1:未捕获的语法错误:意外的令牌导出
- php - 我们如何将转换后的图像分辨率设置为与 imagemagick 中的源文件相同?
- javascript - 使用 JQuery Ajax 将 Javascript 数组传递给控制器
- c# - 在Unity C#中计算meshCollider是否需要为Convex或Concave?
- go - Gin-Gonic(golang)服务器上的 Axios POST 不工作
- java - 我的应用程序没有在手机或模拟器上运行
- node.js - 使用节点 JS 的 Spring Boot SSL
- php - 会话未传递给视图 laravel
- python - Django REST 框架:备用字段名称
- java - 如何在 SWT 表中显示 CCombo 组合框