skip to Main Content

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


  1. 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?

    Login or Signup to reply.
  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 🙂

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search