2011-04-18 32 views
2

在我的MVC 3应用程序,我有一个非常基本的控制器FormsAuthentication.SetAuthCookie问题

[HttpPost] 
public ActionResult SignIn(LogonModel logonModel) 
{ 
    string logonMessage; 
    if(_authenticationService.Login(logonModel.UserName, logonModel.Password, 
     out logonMessage)) 
    { 
     FormsAuthentication.SetAuthCookie(logonModel.UserName,true); 

     return RedirectToAction("Index", "Home"); 
    } 

    return View(); 
} 

我看到cookie的浏览器越来越设置但是当我关闭浏览器,然后回来到现场,这是不自动登录我。这几乎就像cookie没有被处理,它应该是。

回答

4

第一个也是最明显的问题是:在您的浏览器中是否启用了Cookie

你可以的是检查什么为好,无论是在你的web.config,你已经配置了<authentication>部分有暂停:

<authentication mode="Forms"> 
    <forms loginUrl="~/login" timeout="60" /> 
</authentication> 

如果不指定超时,它会使用默认值30分钟,但也许你把它设置为一些其他(无效)的价值?

timeout属性:

指定的时间,以整数分钟,之后cookie的过期。

您还可以检查CookiesSupported propertyboolean)以查看是否返回。

+0

我将此标记为答案,因为它得到我回头看着web.config。我在Cookie中有一个错字。现在它似乎工作。我还没有测试过所有的浏览器,但它在Chrome中起作用。非常感谢......我爱错别字。 – 2011-04-18 16:45:09

+0

嘿嘿,我可以“帮助”:) – 2011-04-18 17:31:17

+0

有趣的是,我如何回顾这些,并意识到我今天有多好的开发人员......我永远不会问这个今天迟钝的东西:) – 2012-05-08 21:29:31

3

确保从web.config中删除以下<modules>标签如果使用MVC 5+

<system.webServer> 
<modules> 
    <remove name="FormsAuthentication" /> 
</modules> 
</system.webServer> 
0

我的情况下,该域名是错误的authenticaion部分:

<authentication mode="Forms"> 
     <forms name="yourAuthCookie" loginUrl="/user/login" protection="All" path="/" enableCrossAppRedirects="true" timeout="90" domain="WRONGDOMAIN.COM" /> 
    </authentication> 

删除域没有诀窍:

<authentication mode="Forms"> 
     <forms name="yourAuthCookie" loginUrl="/user/login" protection="All" path="/" enableCrossAppRedirects="true" timeout="90" /> 
    </authentication> 
相关问题