I am working on ASP.NET MVC5 application and I want to implement RememberMe functionality. Here, when the user check RememberMe then, the username should be automatically fetch to username field, whenever the user go to the website again. I have tried with following methods. But it has problem with it and i am not able to done this. Can anyone help me to do this?
var authTicket = new FormsAuthenticationTicket(
1,
userId, //user id
DateTime.Now,
DateTime.Now.AddMinutes(50), // expiry
model.RememberMe, //true to remember
string.Empty
);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
if (authTicket.IsPersistent)
{
cookie.Expires = authTicket.Expiration;
}
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
Thanks in advance.
I have done this by using following code. In Controller: In Login POST action:
if(model.RememberMe == true)
{
Response.Cookies["email"].Value = model.Email;
Response.Cookies["email"].Expires = DateTime.Now.AddDays(365);
}
In Login Initial Action:
if (Request.Cookies["email"] != null)
ViewBag.email = Request.Cookies["email"].Value;
In View:
@if (ViewBag.email != null)
{
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @Value = ViewBag.email })
}
else
{
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
}