I have some issues with my .NET CORE 2.1 web api. Im using Identity framework and JWT-tokens for authentication, but whenever I try to get the current user in a controller I get a null reference error.
All settings look fine from what I can see, and I've tried to manually set the key for Identity, but still getting the same error. Any ideas?
Controller:
var user = await _userManager.GetUserAsync(User);
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<LoginUser, IdentityRole>(options => {
options.ClaimsIdentity.UserIdClaimType =
ClaimTypes.NameIdentifier;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
Claims in token-generation:
var claims = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Sub, user.Id),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(JwtRegisteredClaimNames.Email, email),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};