![](/img/trans.png)
[英]User.Identity.Name is null in my ASP.NET Core Web API
[英]User.Identity.Name is null for Blazor Wasm hosted in asp.net core
对于托管在 asp.net core 中的 Blazor Wasm,User.Identity.Name 为 null。 没有诸如姓名或电子邮件之类的声明。 我正在使用 Visual Studio 的默认模板。
services.AddIdentityServer().AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication().AddIdentityServerJwt();
我错过了什么吗?
如果您需要更多信息,请与我们联系。
您的代码没有任何实际意义。 您不会在 blazor 应用程序中托管 IdentityServer,而是有一个单独的专用服务来实现 OpenIDConnect,并且可以验证用户并提供令牌。
在 blazor 应用程序中,您通常使用这种类型的框架来配置身份验证:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(opt =>
//Add configuration here
{
}).AddOpenIdConnect(options =>
{
//Add configuration here
});
几个月来,我一直在为同样的问题苦苦挣扎。 在尝试了很多方法后,我终于找到了解决方案。
using System.Linq;
services.AddIdentityServer().AddApiAuthorization<ApplicationUser,
ApplicationDbContext>(opt =>
opt.IdentityResources["openid"].UserClaims.Add("name");
opt.ApiResources.Single().UserClaims.Add("name");
});
services.AddAuthentication().AddIdentityServerJwt();
请让我知道这是否解决了问题。 谢谢:)
我在添加到我的 Blazor WASM 托管服务器项目的 API 控制器中遇到了同样的问题。 将此添加到我的program.cs
文件中是为我修复的:
services.Configure<IdentityOptions>(options => options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);
来源: https ://github.com/dotnet/AspNetCore.Docs/issues/17517
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.