.NET Core Cookie Authentication SignInAsync not working

as of .net 2.x, if you’re using cookie auth, ensure you include the authenticationScheme and specify name and role claim types, the identity and auth properties.

//You should specify the 'types' of the claims which acts as 'name' and 'role', furthermore, you should have claims with those types in your claimsIdentity like that
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
        
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, email));
identity.AddClaim(new Claim(ClaimTypes.Name, email));
identity.AddClaim(new Claim(ClaimTypes.Role, "User"));

var principal = new ClaimsPrincipal(identity);

var authProperties = new AuthenticationProperties
{
    AllowRefresh = true,
    ExpiresUtc = DateTimeOffset.Now.AddDays(1),
    IsPersistent = true,
};
        
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(principal),authProperties);

return RedirectToPage("dashboard");

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)