简体   繁体   English

ASP.NET表单身份验证Cookie

[英]ASP.NET Forms Authentication Cookie

We try to implement ASP.Net Forms Authentication. 我们尝试实现ASP.Net表单身份验证。

Everything works in our Development environment/server. 一切都在我们的开发环境/服务器中工作。 But when we released to Production, we noticed that the cookies don't work properly in FireFox and Chrome. 但是,当我们发布到Production时,我们注意到Cookies在FireFox和Chrome中无法正常工作。 IE11 and Safari (Mac OSX) do work. IE11和Safari(Mac OSX)可以正常工作。

When I view the 'Cookies set by this page' (Chrome), I can see the cookie (both in Development as well as Production environment) 当我查看“此页面上设置的Cookie”(Chrome)时,可以看到Cookie(在“开发”环境和“生产”环境中)

But when I check the development tools (Chrome) there is no Cookie on when I test on Production, but there is a Cookie when I test on Development. 但是,当我检查开发工具(Chrome)时,在生产环境中进行测试时没有Cookie,但是在开发环境中进行测试时却存在Cookie。

When I do a request to check 'Context.User.Identity.IsAuthenticated', the Production environment returns false, while the development environment returns true. 当我请求检查“ Context.User.Identity.IsAuthenticated”时,生产环境返回false,而开发环境返回true。

The code is identical on the 2 servers: 这两个服务器上的代码相同:

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!!";
    }

And so is the Web.config Web.config也是

<authentication mode="Forms">
      <forms name="TestingSession" cookieless="UseCookies" protection="All" timeout="30" ></forms>
    </authentication>

Why is this not working in Chrome and FireFox in my Production environment, when it does work in IE11 and Safari (on Mac OSX). 当它在IE11和Safari(在Mac OSX上)有效时,为什么在我的生产环境中的Chrome和FireFox中不起作用。

And why does it work in all the browsers I tested with in my Development environment? 为什么在我在开发环境中测试过的所有浏览器中都可以使用它? Is it an IIS setting? 是IIS设置吗? Server issue? 服务器问题? Or am I missing something else. 还是我想念其他东西。

I hope someone can help me out. 我希望有人能帮助我。

EDIT: 03-03-2014 编辑:03-03-2014

After some more testing I noticed the Response Header Date is wrong. 经过更多测试后,我注意到响应标题日期是错误的。

It is always: Tue, 21 Oct 2014 18:04:35 GMT 永远是:2014年10月21日,星期二,格林尼治标准时间

The date does not change when the page is called again or in another browser. 再次调用页面或在其他浏览器中,日期不会更改。

This means the Cookie is already expired when it is returned to the browser? 这意味着Cookie返回浏览器时已经过期了吗?

I already checked IIS7 for custom headers, but found none. 我已经在IIS7中检查了自定义标头,但没有找到。

We also reset the Http Service on the server but still no luck. 我们还重置了服务器上的Http服务,但仍然没有运气。

We solved the issue by restarting the server. 我们通过重新启动服务器解决了该问题。

After some more research we found that the Response Header Date was the same date as a previous issue we had. 经过更多研究后,我们发现“响应标题日期”与我们上一期的日期相同。

A few months ago we added some more RAM to the server. 几个月前,我们向服务器添加了更多RAM。 After that, the server date was set in the future (21 oct. 2014). 之后,服务器日期将被设置为将来的日期(2014年10月21日)。 We noticed that issue very fast and set the date back to to actual date. 我们注意到该问题非常迅速,并将日期设置回实际日期。

We never preformed a restart of the server at that point. 那时我们从未执行过服务器的重启。 It seems that the Date returned in the Response Headers did require a restart. 似乎响应标题中返回的日期确实需要重新启动。

Steps we took to resolve this issue. 我们已采取步骤解决此问题。

  • Make sure the Server date is the actual date. 确保服务器日期是实际日期。
  • Reset the HTTP Service on the Server. 在服务器上重置HTTP服务。
  • Restart the Server. 重新启动服务器。

I hope this helps others with the same issue. 我希望这对其他人也有帮助。

For anybody landing on this page: 对于登陆此页面的任何人:

@Mithrodin's answer does resolve the issue but this issue can occur in future if at all there's change in DateTime on application server ever. @Mithrodin的回答确实解决了该问题,但是如果应用服务器上的DateTime根本没有变化,则将来可能会发生此问题。 This could be intentional or because of Daylight Saving Time effect. 这可能是有意的,或者是由于“夏令时”的影响。

This issue is specific when one use below method to set permanent cookie: 当使用以下方法设置永久cookie时,此问题是特定的:

FormsAuthentication.SetAuthCookie("TESTER", true);

Solution to avoid this issue in future is pass false as second argument in above method. 将来避免此问题的解决方案是将false作为上述方法的第二个参数传递。 As cookie won't be permanent, please set appropriate timeout in FormsAuthentication tag in web.config to make it work. 由于Cookie不会永久存在,因此请在web.config的FormsAuthentication标记中设置适当的超时,以使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM