在mvc3中创建一个记住我的功能

时间:2013-08-30 08:04:33

标签: asp.net-mvc-3

如何在用户点击时记住我创建存储用户名和密码的cookie?复选框。

1 个答案:

答案 0 :(得分:0)

为了记住我的功能你的行动会像这样

[HttpPost]
public ActionResult Login(LoginModel model) {

 //Implementaion for authorization
.......

var authTicket = new FormsAuthenticationTicket(
  1,
  userId,  //user id
  DateTime.Now,
  DateTime.Now.AddMinutes(20),  // expiry
  rememberMe,  //boolean value true for remember
  "", //roles 
  "/"
);

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,   FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);

return RedirectToAction("Index");
}
相关问题