【问题标题】:MVC3 FormsAuthentication cookie not obeyed in Firefox and Chrome, works in IE在 Firefox 和 Chrome 中不遵守 MVC3 FormsAuthentication cookie,在 IE 中有效
【发布时间】:2012-12-17 08:36:45
【问题描述】:

我被难住了。我有一个 MVC3 应用程序,它使用 FormsAuthentication 和自定义令牌(存储一些额外的用户数据)。它适用于 IE。但是,在 Firefox 和 Chrome 中,不遵守 cookie。

在 Fiddler 中的检查表明,登录后 cookie 在响应中发送到客户端,但在客户端的后续请求(登录后加载主页)中,cookie 不会发送回服务器。

我的代码:

var encryptedCookieString = FormsAuthentication.Encrypt(ticket);
  var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookieString);
  cookie.Expires = ticket.Expiration;
  HttpContext.Current.Response.Cookies.Add(cookie);

并获取 cookie:

var cookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);

还有用于 Auth 设置的 web.config:

 <authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

我查看了 Firefox 和 Chrome 浏览器,但没有看到任何关闭 cookie 的地方。

起初我认为这可能是在 localhost 上开发的问题,所以我部署到了培训服务器。同样的故事 - 适用于 IE,不适用于 Chrome 或 Firefox。

有什么线索吗?

更新

我的一位同事能够使用 Chrome 访问它,并且对他有用。所以我不得不相信这是我的 Chrome/Firefox 的错误。此外,我在 VPN 上并在我的开发机器中使用远程桌面。这会导致问题吗?

【问题讨论】:

    标签: asp.net google-chrome firefox cookies formsauthentication


    【解决方案1】:

    答案是:cookie 太大了。

    我花了一些时间才弄清楚这一点。我们在 cookie 中存储了大量额外的用户数据(以防止对数据库的重复查询),并且 cookie 超过了大多数浏览器允许的 4Kb。

    【讨论】:

    • 是的,这确实是我的问题。
    【解决方案2】:

    是否有可能在 web.config 的身份验证的 forms 节点中有 cookieless="UseDeviceProfile""AutoDetect"

    如果是,请将其更改为cookieless="UseCookies"

    【讨论】:

    • 不幸的是,没有。它只是空白。我将使用我的 web.config 为 Auth 设置更新问题。
    • 您是否尝试强制它使用Cookies添加cookieless="UseCookies"?
    • 这很奇怪,我的做法和你差不多。我以这种方式获取 cookie 并在 Application_PostAuthenticateRequest(object sender, EventArgs e) 上设置主体对象
    猜你喜欢
    • 2014-09-10
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 2013-02-20
    相关资源
    最近更新 更多