登录MVC3应用程序不记得我是谁

时间:2011-06-29 15:11:34

标签: asp.net asp.net-mvc-3 login

我有一个使用NHibernate的MVC3应用程序。我创建了自己的会员和角色提供者,他们工作正常。我有登录的问题(可能是由于缺乏ASP.NET安全性)。我想知道是否有人可以帮助我。

这是我的控制器

    [Authorize(Roles="Admin")]
    public ActionResult Edit(int? id)
    {
       ...

我在Web.config中设置了登录网址

<authentication mode="Forms">
  <forms loginUrl="~/User/Login" timeout="2880" cookieless="AutoDetect" />
</authentication>

因此,当我尝试单击编辑链接时,我会转到登录页面。到目前为止一切顺利。

    [HttpPost]
    public ActionResult Login(LogOnModel model, string returnUrl)
    {

        if (ModelState.IsValid)
        {
            if (_membershipProvider.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.Authenticate(model.UserName, model.Password);        
                FormsAuthentication.RedirectFromLoginPage(model.UserName, false);
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        return View(model);
    }

然后我转发到我来自的页面但是当我再次单击“编辑”链接时,我会被发送回登录页面。如何让ASP.NET将用户存储在会话中,以便让它记住我?

1 个答案:

答案 0 :(得分:1)

你应该将“true”传递给:

FormsAuthentication.RedirectFromLoginPage(model.UserName, true);

从这里开始:http://msdn.microsoft.com/en-us/library/ka5ffkce(v=VS.100).aspx

  

userName 经过身份验证的用户名。

     

createPersistentCookie 如果为true,则创建一个   持久的cookie(一个被保存的   跨浏览器会话);除此以外,   假的。