brstkr
brstkr

Reputation: 1623

ClaimTypes.NameIdentifier returns always null in Blazor WebAssembly ASP.NET Core Hosted

I have setup a Blazor WebAssembly ASP.NET Core hostedapplication and integrated the authentication system as well as the custom AuthenticationProvider to the client. In the server side project I am calling in the controller this:

[HttpGet]
public async Task<ActionResult> GetObjects()
{
   var id = User.FindFirstValue(ClaimTypes.NameIdentifier); // <-- returns always null
}

and the id variable is always returning null.

The user is from the aspnetcoreuser table (aspnetcore identity) In the table data I can see, as I am using SQLite, that the id is not null and ther users got added succesfully to the aspnetuser table.

Some SDK-Versions:

.NET Core SDKs installed: 3.1.301 .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.19 Microsoft.AspNetCore.App 2.1.19Microsoft.AspNetCore.App 3.1.5 Microsoft.NETCore.App 2.1.19 Microsoft.NETCore.App 3.1.5 Microsoft.WindowsDesktop.App 3.1.5

Also I have seen some solutions that the "windowsAuthentication": true, "anonymousAuthentication": false, from the launchSettings.json can cause that but with changes it does not solved it for me.

Anyone here who can help me out?

Upvotes: 2

Views: 3932

Answers (1)

nahidf
nahidf

Reputation: 2394

You need to set UserIdClaimType on the server app by adding this code to Startup.ConfigureServices:

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

read more here

Upvotes: 2

Related Questions