【发布时间】:2014-04-01 17:47:08
【问题描述】:
我们尝试实现 ASP.Net 表单身份验证。
在我们的开发环境/服务器中一切正常。但是当我们发布到生产环境时,我们注意到 cookie 在 FireFox 和 Chrome 中无法正常工作。 IE11 和 Safari (Mac OSX) 可以工作。
当我查看“此页面设置的 Cookie”(Chrome)时,我可以看到 cookie(在开发环境和生产环境中)
但是当我检查开发工具(Chrome)时,我在 Production 上测试时没有 Cookie,但在 Development 上测试时却有 Cookie。
当我请求检查“Context.User.Identity.IsAuthenticated”时,生产环境返回 false,而开发环境返回 true。
两台服务器上的代码相同:
protected void Page_Load(object sender, EventArgs e)
{
this.StatusLabel.Text = "Authorized : " + Context.User.Identity.IsAuthenticated.ToString();
}
protected void SetCookieButton_Click(object sender, EventArgs e)
{
FormsAuthentication.SetAuthCookie("TESTER", true);
}
protected void DeleteCookieButton_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
}
protected void AuthorizedRequiredButton_Click(object sender, EventArgs e)
{
if (Context.User.Identity.IsAuthenticated)
this.StatusLabel.Text = "SUCCESS!!" + User.Identity.Name;
else
this.StatusLabel.Text = "NOT AUTHORIZED!";
}
protected void AuthorizedNotRequiredButton_Click(object sender, EventArgs e)
{
this.StatusLabel.Text = "SUCCESS!!";
}
Web.config 也是如此
<authentication mode="Forms">
<forms name="TestingSession" cookieless="UseCookies" protection="All" timeout="30" ></forms>
</authentication>
为什么它不能在我的生产环境中的 Chrome 和 FireFox 中运行,而它在 IE11 和 Safari(在 Mac OSX 上)中运行。
为什么它可以在我在开发环境中测试的所有浏览器中工作?它是IIS设置吗?服务器问题?还是我错过了其他东西。
希望有人能帮帮我。
编辑:2014 年 3 月 3 日
经过更多测试后,我注意到响应标头日期错误。
总是:格林威治标准时间 2014 年 10 月 21 日星期二 18:04:35
再次调用页面或在其他浏览器中调用时,日期不会更改。
这表示Cookie返回浏览器时已经过期了?
我已经检查了 IIS7 的自定义标头,但没有找到。
我们还重置了服务器上的 Http 服务,但仍然没有成功。
【问题讨论】:
-
您能否确保 Dev 和 Pro 都具有相同的 web.config 设置 (连接字符串除外) 和相同的 .Net Framework ?
-
Web.config 和 .Net Framework 是一样的。两台服务器上的几乎所有内容都相同,除了数据库内容。我开始认为它必须是某个地方的设置。
-
“这意味着 Cookie 在返回到浏览器时已经过期” - 你所拥有的日期是 提前 时间的,为什么这会使 cookie 过期吗?
-
@James 我自己也不确定。我将 Web.config 中的超时更改为较高的值(3000000)。这行得通。 cookie 和 ASP.NET 表单身份验证按预期工作。但我显然不希望超时时间这么长。
-
您使用的是哪个版本的 ASP.NET?
30应该绰绰有余,也就是 30 分钟。
标签: asp.net google-chrome cookies