ASP.Net Identity with IdentityServer fails to get User in controller context

Origin

I followed the documentation here and was able to get my controllers working with the [Authorize] header. I am using IdentityServer with ASP.NET Identity as my user store.

In my ConfigureServices I have:

services.AddIdentity<KipUser, IdentityRole>()
     .AddEntityFrameworkStores<KipDbContext>()
     .AddDefaultTokenProviders();

In my Configure I have:

app.UseIdentityServer();
app.UseAuthorization();

And in my controller I have done a few tests:

[Authorize]
public IEnumerable<MyDTO> GetData(int count = 3) {
     var test = User; // The User.Identity.Name is empty for some reason
     var id = User.FindFirst(ClaimTypes.NameIdentifier).Value; // Successfully gets the Guid
     var test2 = _userManager.GetUserAsync(User); // result is null
     var test3 = _userManager.GetUserId(User); //returns null
     var test4 = _userManager.FindByIdAsync(id); // Successfully gets the User from the DB

It seems there is a misconfiguration because the UserManager fails to get the ASP.Net Identity from the ClaimsPrincipal User variable. Am I assuming too much that the .AddEntityFrameworkStores<KipDbContext>() would configure the application to know how to _userManager.GetUserAsync(User) instead of me needing to search for the claim myself with this _userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value) ?

Origin

The main reason I asked this question is because I saw lots of information about this for IdentityServer 2 and 3 that seemed related. I couldn't get those fixes to work in my IdentityServer 4 project so I thought it was a different issue. It is the same issue. As @Tore pointed out, it's an issue with the claims, but I assumed that this would be taken care of with app.UseIdentityServer();

This describes the issue clearly:

UseIdentity and UserManager disagree on where the user ID claim is stored

Even though we are calling app.UseIdentityServer(); it seems there are some default mappings put in by Microsoft that need to be cleared out.

Per that issue, adding JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); in the Startup.cs Configure function before app.UseIdentityServer(); fixed the problem for me. I can now see the claims are unchanged in the controller, and both _userManager.GetUserAsync(User); and _userManager.GetUserId(User); work as expected.

Surprisingly, this function call is not included in the IdentityServerAspNetIdentity sample project.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Web API get user identity in controller constructor

How does Asp.Net Identity 2 User Info get mapped to IdentityServer3 profile claims

ASP.NET Identity2 - How to get User Id with AllowAnonymous Controller?

How to get logged-in user name in the constructor of a controller in ASP.NET Core Identity

IdentityServer4 + Asp.Net Core Identity - Map Identity to application database user

How to get the OWIN context on ASP.Net Identity?

Owin Context on Asp .Net Identity

ASP.NET Identity and IdentityServer connection

asp.net core identity and identityserver

IdentityServer4 can't add asp net core identity with custom user store

how to get Identity PasswordOptions in ASP.NET MVC controller

ASP.NET Identity (with IdentityServer4) get external resource oauth access token

ASP.NET Core Identity - get current user

Get current user id in ASP.NET Identity 2.0

Get ASP.NET Identity Current User In View

Get Authenticated Identity User data in ASP.NET MVC

Get User Roles with ASP.net Identity and Web API

How to get Asp.net Core Identity User in View

How to get role name for user in Asp.Net Identity

Adding Claims to an ASP.Net Identity Core user fails with EF insert error

How to disable account if user login fails continuously in Asp.Net Core identity

Asp.Net Core Identity 2.2 and Identity Server 4, Changing User Id type results in an error in the db context

Get current user in ASP.NET 5 beta 6 controller

IdentityServer4 Asp.Net Core Identity

IdentityServer4 with ASP.Net Identity Project Architecture

IdentityServer 3 + Asp.net Identity: Scopes, Claims and Clients - Clarifications

what is the difference between ASP.NET Identity and IdentityServer?

ASP.NET Core Identity, IdentityServer4 and Changing a password

Identityserver and asp.net identity in multitenant spa application and authorisation