【问题标题】:Create a Remember me functionality in mvc3在 mvc3 中创建记住我的功能
【发布时间】:2013-09-02 21:20:25
【问题描述】:

当用户点击记住我时,如何创建一个 cookie 来存储用户名和密码?复选框。

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    对于记住我的功能,你的操作将像这样进行

    [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");
    }
    

    【讨论】:

    • 在你从其他地方复制代码之前,你至少应该告诉他们你从哪里得到它。在这种情况下,只需重定向到源:stackoverflow.com/questions/5619791/…
    • 谢谢 bt 它的 rememberMe 值始终为 false,rememberMe 不是类的属性。
    • 您必须在课堂上添加rememberMe。因此,在创建票证时,将设置适当的值。
    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2015-10-17
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    相关资源
    最近更新 更多