How to check if user is authenticated in Razor pages of .Net Core 2.0
I've always used this option.
private readonly SignInManager<IdentityUser> _signInManager;
public HomeController(SignInManager<IdentityUser> signInManager)
{
_signInManager = signInManager;
}
public IActionResult Index()
{
if (_signInManager.IsSignedIn(User)) //verify if it's logged
{
return LocalRedirect("~/Page");
}
return View();
}
Hope it helps someone!
Edit: David is right of course.
Just check if User
or HttpContext.User.Identity.IsAuthenticated
is true
or not.
@if(!User.Identity.IsAuthenticated)
{
...
}