I am trying to get the current login user in the base controller
public abstract class BaseController : ControllerBase { private readonly UserManager<ApplicationUser> _userManager; public BaseController() { var user = _userManager.FindByNameAsync(User.Identity.Name).Result; } } However, the user is null, so User.Identity.Name is created null pointer exception.
I am using the Asp.net core 3.1 with Angular template from visual studio and Identity server as
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="3.1.8" /> <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.8" /> StartUp.cs
services.AddIdentityServer() .AddApiAuthorization<ApplicationUser, ApplicationDbContext>() .AddProfileService<ProfileService>(); services.AddAuthentication() .AddIdentityServerJwt(); app.UseAuthentication(); app.UseIdentityServer(); app.UseAuthorization(); app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); Profile.service
public async Task GetProfileDataAsync(ProfileDataRequestContext context) { var user = await _userManager.GetUserAsync(context.Subject); var claims = new List<Claim> { new Claim("FullName", $"{user.FirstName} {user.LastName}" ), new Claim("Email", user.Email ), new Claim("UserId", user.Id), new Claim("name", user.Email), }; context.IssuedClaims.AddRange(claims); }