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就可以了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界