HttpContext.User.Claims and IHttpContextAccessor both returns empty value after successful login in .NET Core 2.2 Here my Startup Services,
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
,b=>b.MigrationsAssembly("AdaptiveBizapp")));
services.AddDbContext<Project_Cost_Management_SystemContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Project_Cost_Management_SystemContext")
, b => b.MigrationsAssembly("AdaptiveBizapp")));
services.AddDefaultIdentity<ApplicationUser>()
.AddRoles<ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.ConfigureApplicationCookie(options => {
options.LoginPath = "/Account/login";
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
// Make the session cookie essential
options.Cookie.IsEssential = true;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
and my Configure section,
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "area",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
});

I used Identity and Role based authorization. After login successful, in HomeController when i read user claims or NameIdentifier is empty. But when i read in same LoginController It has value at ClaimPrincipal,
public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal =await base.CreateAsync(user);
// Add your claims here
((ClaimsIdentity)principal.Identity).
AddClaims(new[] {
new System.Security.Claims.Claim(ClaimTypes.NameIdentifier,
user.UserName.ToString())
});
return principal;
}



Searched all day and finally figured it out. The claims are hydrated on the next call to the server. Hopefully this post helps someone else out. If you follow microsofts documentation you are good but just dont try to get the claims in the same call as when you set them. Do another call and they will be hydrated.
if you want use Depency Injection for IHttpContextAccessor you need to add :
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
...
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With