4

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.

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Also one of the thing: AddIdentity instead of AddDefaultIdentity to get User.Identity.Name.
2

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

Comments

0

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?

3 Comments

Blazor Wasm is hosted inside of ASP.NET Core Web API and this server has IdentityServer inside. (I'm not sure about that, but it is implied by the resources I read on msdn)
Also I haven't the skeleton presented by you.
I would still put IdentityServer in a separate ASP.NET core service, because it will be much harder to reason and troubleshoot it from my experience. I really like the separation of concerns is an important rule here. Because as a developer you really need to understand what is going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.