简体   繁体   中英

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. There is no such claim as name or email. I'm using default template of 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.

In a blazor application you typically have this type of skeleton to configure authentication:

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?

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. Adding this to my program.cs file is what fixed it for me:

services.Configure<IdentityOptions>(options => options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);

Source: https://github.com/dotnet/AspNetCore.Docs/issues/17517

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM