简体   繁体   English

登录时循环自定义声明 - Asp.net 核心

[英]Looping custom claims on login - Asp.net core

Our ASP.NET MVC application connects to IdentityServer 3 with the following config and able to access all the custom claims我们的 ASP.NET MVC 应用程序使用以下配置连接到 IdentityServer 3,并且能够访问所有自定义声明

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                Authority = IdentityServerUrl,
                ClientId = IdentityClientId,                              
                ResponseType = "id_token token",
                Scope = "openid profile myScope",
                SignInAsAuthenticationType = "Cookies",

                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = async n =>
                    {
                        var newIdentity = new ClaimsIdentity(
                            n.AuthenticationTicket.Identity.AuthenticationType,
                            "name",
                            "myrole");

                        var userInfoClient = new UserInfoClient(
                            new Uri(n.Options.Authority + "/connect/userinfo"),
                            n.ProtocolMessage.AccessToken);

                        var userInfo = await userInfoClient.GetAsync();
                        userInfo.Claims.ToList().ForEach(ui => newIdentity.AddClaim(new Claim(ui.Item1, ui.Item2)));

                        var sid = n.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type == "sid");
                        if (sid != null)
                        {
                            newIdentity.AddClaim(new Claim("sid", sid.Value));
                        }

                        n.AuthenticationTicket = new AuthenticationTicket(
                            newIdentity,
                            n.AuthenticationTicket.Properties);
                    }
                }
            });

Now we want to upgrade and connect to IdentityServer 3 with .net core现在我们要升级并连接到带有 .net 核心的 IdentityServer 3

We tried below code but I am not getting the sure how to loop through all the custom claims我们尝试了下面的代码,但我不确定如何遍历所有自定义声明

.AddOpenIdConnect("oidc", options =>
                {
                    options.Authority = IdentityClientUrl;
                    options.ClientId = IdentityClientId;
                    options.ResponseType = OpenIdConnectResponseType.IdTokenToken;
                    options.Scope.Clear();
                    options.Scope.Add("profile");
                    options.Scope.Add("openid");
                    options.Scope.Add("email");
                    options.Scope.Add("myScope");

                    options.GetClaimsFromUserInfoEndpoint = true;

                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        NameClaimType = "name",
                        RoleClaimType = "myrole"
                    };

                    options.SaveTokens = true;
                    options.ClaimActions.MapUniqueJsonKey("myrole", "myrole", "string");          
                });

In the existing approach, i am able to get all the claims from userInfo , so I can loop and add everything.在现有方法中,我能够从userInfo获取所有声明,因此我可以循环并添加所有内容。 In the asp.net core - however I can map them using ClaimActions, eachone at a time.在 asp.net 内核中 - 但是我可以一次使用 ClaimActions map 他们。 Is there any way I can loop throug all of them and add all of them - say I don't know the claimType!有什么办法可以循环遍历所有这些并添加所有这些 - 说我不知道claimType!

Any help please?请问有什么帮助吗?

You can try this to map all the claims using the MapAllExcept method, like:您可以使用 MapAllExcept 方法对 map 的所有声明进行尝试,例如:

options.ClaimActions.MapAllExcept("iss", "nbf", "exp", "aud", "nonce");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM