Unable to Retrieve Claims in .NET Core 2.0

前端 未结 2 1423
借酒劲吻你
借酒劲吻你 2021-02-09 05:34

I am using an OpenId Connect Authentication Server, specifically Identity Server 4 (version 1.5.2) on .NET Core 1.1. I have this running with ASP.NET

2条回答
  •  离开以前
    2021-02-09 05:45

    Please see below, I ran into the same issue as you. I'm sure there is a configuration issue we are missing, but for the time being I parsed the values coming back from the UserInfoEndpoint in the OnUserInformationReceived event handler.

        public override void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
                .AddCookie()
                .AddOpenIdConnect(o =>
                {
                    o.ClientId = "sss";
                    o.ClientSecret = "sss";
                    o.RequireHttpsMetadata = false;
                    o.Authority = "http://localhost:60000/";
                    o.MetadataAddress = "http://localhost:60000/IdSrv/.well-known/openid-configuration";
                    o.ResponseType = OpenIdConnectResponseType.IdTokenToken;
                    o.CallbackPath = new PathString("/CGI/Home/Index");
                    o.SignedOutCallbackPath = new PathString("/CGI/Account/LoggedOut");
                    o.Scope.Add("openid");
                    o.Scope.Add("roles");
                    o.SaveTokens = true;
                    o.GetClaimsFromUserInfoEndpoint = true;
                    o.Events = new OpenIdConnectEvents()
                    {
                        OnUserInformationReceived = (context) =>
                        {
                            ClaimsIdentity claimsId = context.Principal.Identity as ClaimsIdentity;
    
                            var roles = context.User.Children().FirstOrDefault(j => j.Path == JwtClaimTypes.Role).Values().ToList();
                            claimsId.AddClaims(roles.Select(r => new Claim(JwtClaimTypes.Role, r.Value())));
    
                            return Task.FromResult(0);
                        }
                    };
                    o.TokenValidationParameters = new TokenValidationParameters
                    {
                        NameClaimType = JwtClaimTypes.Name,
                        RoleClaimType = JwtClaimTypes.Role,
                    };
                });
        }
    

    Edit:

    I found that there is an extension method for ClaimsAction property called MapUniqueJsonKey, this seems to work for custom single valued keys but bombs on array types like roles... getting closer

    o.ClaimActions.MapUniqueJsonKey("UserType", "UserType");
    

提交回复
热议问题