Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 564
David Olejnik Avatar asked Oct 31 '25 23:10

David Olejnik


2 Answers

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 :)

like image 127
Fitri Halim Avatar answered Nov 03 '25 12:11

Fitri Halim


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

like image 33
Drew Brasher Avatar answered Nov 03 '25 14:11

Drew Brasher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!