KaviSuja KaviSuja - 2 months ago 25x
ASP.NET (C#) Question

Remember Me - Fetch UserName - MVC

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(
userId, //user id
DateTime.Now.AddMinutes(50), // expiry
model.RememberMe, //true to remember

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
if (authTicket.IsPersistent)
cookie.Expires = authTicket.Expiration;

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 })
     @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })