I am quite confused as how to implement this functionality. Initially, when the user visits the website, they see the sign in page.
[HttpGet]
public ActionResult SignIn()
{
return View() ;
}
When the user enters the details, it calls:
[HttpPost]
public ActionResult Sign(SignInModel signInModel)
{
if(service.ValidateUser(signInModel.userName,signInModel.passWord))
{
FormsAuthentication.SetAuthCookie(signInModel.userName, true);return
return RedirectToAction("Index", "Home");
}
}
However, as a test I decided to go back to the log in page, so localhost/Account/SignIn, but it doesn't redirect me back to the home page.. so I tried some suggested answers to similar questions from SO:
[HttpGet]
public ActionResult SignIn()
{
if(HttpContext.User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Home");
}
return View() ;
}
But I didn't understand how it worked so I decided to debug it. But It turns out it was using the wrong Identity
.
To explain, I was using a default MVC template to get my project working, I logged in with James on that template. However with my own project, I logged in with Peter.
But HttpContext.User.Identity
in my own project refers to the default website's James instead of Peter.. so there is obviously something wrong there, but what?
TL;DR how do I persist information like StackOverflow? The user should only see the sign in page when the session expires or the user signs out.