Closed shreesharao closed 6 years ago
Refer https://github.com/aspnet/Security/issues/1538.
comments relevent
SignInManager is part of the Identity framework. The configuration you're showing is not using the Identity framework, it's only using the Authentication components.
SignInManager is looking specifically for the identity application cookie (.AspNetCore.Identity.Application), you only want to use that method if you are using SignInManager to sign in typically.
When i changed the Login action method code to use IdentityConstants.ApplicationScheme for cookies, SignInManager.IsSignedIn(User) returned true
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel loginViewModel)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(loginViewModel.Email, loginViewModel.Password, loginViewModel.Rememberme, lockoutOnFailure: true);
if (result.Succeeded)
{
//set cookie
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Name,loginViewModel.Email),
new Claim(ClaimTypes.Role,$"Administrator")
};
var claimsIdentity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme);
var authProperties = new AuthenticationProperties();
//Make the cookie persisitent if the user wants to
if(loginViewModel.Rememberme)
{
authProperties.IsPersistent = true;
}
await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
return RedirectToAction("Dashboard", "Library");
}
else
{
_logger.LogError($"{result}");
}
}
return View(loginViewModel);
}
LibraryController.cs
public class LibraryController : Controller
{
[HttpGet]
[Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrator")]
public IActionResult Dashboard()
{
return View();
}
}
Refer to this issue - https://github.com/shreesharao/aspnetcore-ConsoleToWeb/issues/14 , for Authentication with Cookies without using AspNetCore Identity.
In the Login action method i had
and in Library controller i had code which was working fine
and in Dashboard view i had below code where SignInManager.IsSignedIn(User) was failing