identityserver4 客户claims里没有用户姓名的问题

客户端代码

复制代码
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            services.AddAuthentication(options => {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
           .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
           .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options => {
               options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
               options.GetClaimsFromUserInfoEndpoint = true;
               options.Authority = "https://localhost:5001";
               options.RequireHttpsMetadata = false;
               options.ClientId = "zac.sms";
               options.ClientSecret = "27db4b3f-9cb0-4020-a12c-19f390e9943a";
               options.ResponseType = OpenIdConnectResponseType.Code;// "code"; //代表
               options.Scope.Clear();
               options.Scope.Add("openid");
               options.Scope.Add("AdminWebApi");
               options.SaveTokens = true;
})
复制代码

identityserver4端代码

复制代码
            services.AddAbpDbContext<ApplicationDbContext>(options => {
                options.AddDefaultRepositories(includeAllEntities: true);// 自动创建仓存
            });

            services.Configure<AbpDbContextOptions>(options => {
                options.UseSqlServer();
            });

            var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;
                // see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
                options.EmitStaticAudienceClaim = true;

            });

            builder.AddConfigurationStore(opt => {
                opt.ConfigureDbContext = context => {
                    context.UseSqlServer(connectionString, sql => {
                        sql.MigrationsAssembly(migrationsAssembly);
                    });
                };
            })
            .AddOperationalStore(opt => {
                opt.ConfigureDbContext = context => {
                    context.UseSqlServer(connectionString, sql => {
                        sql.MigrationsAssembly(migrationsAssembly);
                    });
                };
                opt.EnableTokenCleanup = true;
                opt.TokenCleanupInterval = 30;
            });
            //builder.AddResourceOwnerValidator<MyResourceOwnerPasswordValidator>();

            // in-memory, code config
            //builder.AddInMemoryIdentityResources(Config.IdentityResources);
            //builder.AddInMemoryApiResources(Config.ApiResources);
            //builder.AddInMemoryApiScopes(Config.ApiScopes);
            //builder.AddInMemoryClients(Config.Clients);
            //builder.AddClientStore<CustomerClientStore>();
            //builder.AddProfileService<MyProfileService>();
            // not recommended for production - you need to store your key material somewhere secure
            builder.AddDeveloperSigningCredential();

            services.AddAuthentication();
复制代码

 

但是客户端的

HttpContext.User.Identity.Name 为空

HttpContext.User.Identity.Claims里面也没有

最后解决方案时在identityserver4端的client(表名)里将AlwaysIncludeUserClaimsInIdToken设置为true就可以了

posted @   hello_stone  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示