[英]User.Identity.Name is null for Blazor Wasm hosted in asp.net core
User.Identity.Name is null for Blazor Wasm hosted in asp.net core.对于托管在 asp.net core 中的 Blazor Wasm,User.Identity.Name 为 null。 There is no such claim as name or email.没有诸如姓名或电子邮件之类的声明。 I'm using default template of Visual Studio.我正在使用 Visual Studio 的默认模板。
services.AddIdentityServer().AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication().AddIdentityServerJwt();
Am I missing something?我错过了什么吗?
Let me know if you need more information.如果您需要更多信息,请与我们联系。
Your code does not make any real sense.您的代码没有任何实际意义。 You don't host IdentityServer inside a blazor application, instead you have a separate dedicated services that implements OpenIDConnect and that can authenticate users and give out tokens.您不会在 blazor 应用程序中托管 IdentityServer,而是有一个单独的专用服务来实现 OpenIDConnect,并且可以验证用户并提供令牌。
In a blazor application you typically have this type of skeleton to configure authentication:在 blazor 应用程序中,您通常使用这种类型的框架来配置身份验证:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(opt =>
//Add configuration here
{
}).AddOpenIdConnect(options =>
{
//Add configuration here
});
See this article for details: How do I implement Blazor authentication with OpenID Connect?详见本文: 如何使用 OpenID Connect 实现 Blazor 身份验证?
I had been struggling with the same issue for few months already.几个月来,我一直在为同样的问题苦苦挣扎。 After try many approaches, finally I come out with the solution.在尝试了很多方法后,我终于找到了解决方案。
using System.Linq;
services.AddIdentityServer().AddApiAuthorization<ApplicationUser,
ApplicationDbContext>(opt =>
opt.IdentityResources["openid"].UserClaims.Add("name");
opt.ApiResources.Single().UserClaims.Add("name");
});
services.AddAuthentication().AddIdentityServerJwt();
Please let me know if this solve the issue.请让我知道这是否解决了问题。 Thank you:)谢谢:)
I had this same issue in an API controller I added to my Blazor WASM hosted server project.我在添加到我的 Blazor WASM 托管服务器项目的 API 控制器中遇到了同样的问题。 Adding this to my program.cs
file is what fixed it for me:将此添加到我的program.cs
文件中是为我修复的:
services.Configure<IdentityOptions>(options => options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);
Source: https://github.com/dotnet/AspNetCore.Docs/issues/17517来源: https ://github.com/dotnet/AspNetCore.Docs/issues/17517
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.