After validating the user credentials you can have a code like:
public void SignIn(string userName, bool createPersistentCookie)
{
int timeout = createPersistentCookie ? 43200 : 30; //43200 = 1 month
var ticket = new FormsAuthenticationTicket(userName, createPersistentCookie, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
HttpContext.Current.Response.Cookies.Add(cookie);
}
So your code can be like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(string userName, string passwd, bool rememberMe)
{
//ValidateLogOn is your code for validating user credentials
if (!ValidateLogOn(userName, passwd))
{
//Show error message, invalid login, etc.
//return View(someViewModelHere);
}
SignIn(userName, rememberMe);
return RedirectToAction("Home", "Index");
}
In subsequent requests from the logged in user, HttpContext.User.Identity.Name should contain the user name of the logged in user.
Regards!