微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 IProfileService 设置的其他声明在 MVC 客户端的 OpenIdConnect 处理程序中不可用

如何解决从 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);
                            },}
                    });

这里出了什么问题?如果我需要发布更多代码,请告诉我。

解决方法

在 Identity Server 中注册 MVC 客户端时使用 AlwaysIncludeUserClaimsInIdToken = true

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。