简体   繁体   English

对于托管在 asp.net core 中的 Blazor Wasm,User.Identity.Name 为 null

[英]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.

相关问题 User.Identity.Name 是 null 在我的 ASP.NET 核心 Web ZDB974238714CA8A14A7CE1D0 - User.Identity.Name is null in my ASP.NET Core Web API User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null - User.Identity.Name returns null in ASP.NET Core MVC application User.Identity.Name 在 asp.net core + angular 客户端应用程序中为空 - User.Identity.Name is null in asp.net core + angular client app Blazor WASM Asp.net 核心是否托管 = Blazor 服务器端? - Is Blazor WASM Asp.net core Hosted = Blazor server side? 会话与User.Identity.Name进行比较,以使用Windows身份验证检查ASP.net核心应用程序的登录用户名 - Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication 为什么运行 ASP.NET 6 项目时 `User.Identity.Name` `null`? - Why is `User.Identity.Name` `null` when running ASP.NET 6 project? ASP.NET控制器基类User.Identity.Name - ASP.NET Controller Base Class User.Identity.Name 如何使用 ASP.NET Core Hosted 生成 Blazor wasm 脚手架 CRUD (EF) - How to generate Blazor wasm scaffolded CRUD (EF) wiht ASP.NET Core Hosted 从 Blazor WebAssembly (WASM) 项目调用外国 API(**不是** ASP.Net Core,托管) - Call an foreign API from a Blazor WebAssembly (WASM) Project (**NOT** ASP.Net Core, hosted) User.Identity.Name 在 PasswordSignInAsync MVC .Net Core 3.0 之后始终为 null 并声明计数为 0 - User.Identity.Name always null and claims count 0 after PasswordSignInAsync MVC .Net Core 3.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM