_From @neoffer on Tuesday, September 17, 2019 8:16:13 PM_
User.Identity.Name is null after login
Using .NET Core 3.0 rc1. I created a MVC project using VS 2019 Preview MVC template with Individual Authentication. I created a user using Register form, and Logged in. But User.Identity.Name returns null in my controller. I've tried httpContextAccessor, but it is still null.
Edit: I'm using JWT.
_Copied from original issue: dotnet/core#3391_
The Individual Auth templates don't use jwt, so I'm a little confused by what you mean here?
@blowdart I used the Individual Auth MVC template and converted to use JWT according to the docs (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0).
After that MVC Identity Area works good, but the controllers at the root don't see the authenticated user. User.Identity.Name is null at the root controllers.
My aim is to use the Identity in both MVC and my separate Ionic project.
Well no they won't, not unless you convert the UI to use JWT, which of course, it can't, because nothing will send it JWT tokens.
Where is your JWT token coming from?
I don't send JWT yet. I need both IdentityServerJWT and Cookie auth schemes.
Now, I added [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)] to the top of Controller in Admin area. It redirects to Login, but after login I cannot enter to the HomeController of Admin area.
Of course root HomeController still cannot access logged in user.
Some code from Startup:
```c#
services.AddDefaultIdentity
options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores
services
.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
.AddDeveloperSigningCredential();
services.AddAuthentication()
.AddCookie(options => {
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
})
.AddIdentityServerJwt();
Some code from the HomeController of Admin area:
```c#
[Area("Admin")]
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
What happens if you remove all the identity server stuff? And the calls to AddCookie() and AddIdentityServer. AddDefaultIdentity already adds cookies so you don't need it.
I solved the issue by removing identityserver stuff. JwtBearer works perfectly with default token providers. So I can connect the api with JwtBearer from mobile app, additionally I can use my MVC controllers with the same Identity even using Roles. So my problem is solved but on the other hand IdentityServer stuff does not work well with default Identity.
I ran into this with a new Blazor project targetting .Net Core 3.1, Work Account
My Startup.cs
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.Authority += "/v2.0/";
options.TokenValidationParameters.ValidateIssuer = false;
});
The User.Identity.Name is null only when I use the OpenIdConnectOptions and target the v2 endpoint.
If I comment the services.Configure... block, logout and log back in, I see that the authentication happens against the v1 endpoint and the User.Identity.Name is populated correctly
Hello, I am getting User Identity properties all null for .net core 3.1. I am using jwt token. The token does contains the claims properly , Authentication is being performed properly, but User does not have any claims. Please help.I have checked several times for configuring the jwtbearer token on startup also
Hello, I am getting User Identity properties all null for .net core 3.1. I am using jwt token. The token does contains the claims properly , Authentication is being performed properly, but User does not have any claims. Please help.I have checked several times for configuring the jwtbearer token on startup also
There is an issue even with default angular template. Steps to reproduce:
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
There is an issue even with default angular template. Steps to reproduce:
- dotnet new angular -au individual
- Modify Weatherforecast controller to return UserManager.GetUserId(User)
Observe, that userId is always null. And no other methods of UserManager can return the authenticated user.
The reason is how JwtSecurityTokenHandler works. It will remap the sub claim to be ClaimTypes.NameIdentifier. But HttpContext.User does not know about this and is still trying to load user id from sub claim.
To work around the issue I have added this line into the app startup to disable claim remapping:
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary();
I've been having this issue and your post has helped me fix this, specifically the Inbound claim types that have been breaking the UserID claim were the ones with the keys "nameid" and "sub", no need to delete all of them.
Hope this gets fixed soon, or that we get a way of fixing it.
Same here, Blazor server side 3.1, when I change to the version 2 identity endpoint in startup.cs with this line:
options.Authority += "/v2.0";
@context.User.Identity.Name is empty when trying to display the logged in users name in the _LoginDisplay.razor from the template.
Workaround for me was to use this line instead:
@context.User.Claims.Where(c => c.Type == "name").FirstOrDefault().Value
Same here, Blazor server side 3.1, when I change to the version 2 identity endpoint in startup.cs with this line:
options.Authority += "/v2.0";
@context.User.Identity.Name is empty when trying to display the logged in users name in the _LoginDisplay.razor from the template.
Workaround for me was to use this line instead:
@context.User.Claims.Where(c => c.Type == "name").FirstOrDefault().Value
Using Blazor server 3.1 also. When using v2 endpoint I had to set TokenValidationParameters.NameClaimType = "preferred_username" or "name".
Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.
This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!
Most helpful comment
There is an issue even with default angular template. Steps to reproduce:
Observe, that userId is always null. And no other methods of UserManager can return the authenticated user.
The reason is how JwtSecurityTokenHandler works. It will remap the sub claim to be ClaimTypes.NameIdentifier. But HttpContext.User does not know about this and is still trying to load user id from sub claim.
To work around the issue I have added this line into the app startup to disable claim remapping: