Reputation: 85
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.
Upvotes: 3
Views: 1078
Reputation: 739
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 :)
Upvotes: 1
Reputation: 838
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
Upvotes: 2
Reputation: 19921
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?
Upvotes: 0