C#中的Cookie - Expires属性将不会设置 [英] C# Cookie - Expires property won't set

查看:381
本文介绍了C#中的Cookie - Expires属性将不会设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,在通过Web服务后端应用程序的用户日志。 Web服务将返回我要存储40分钟的Cookie的会话ID,40分钟后的后端应用程序将自动关闭会话。

I have a page where a user logs in to a back-end application via a web service. The web service returns a session ID which I want to store in a cookie for 40 minutes, as after 40 minutes the back-end application automatically closes the session.

我的code写的cookie的:

My code to write the cookie:

private void SetCookie()
{
    Response.Cookies.Add(new HttpCookie("Cookie_SessionID"));
    Response.Cookies["Cookie_SessionID"].Value = ni.NicheSessionID;
    Response.Cookies["Cookie_SessionID"].Expires = DateTime.Now.AddMinutes(40);

    //.... after a few more things
    Response.Redirect(returnUrl);
}

然后在接收页上我有这样的:

Then on the receiving page I have this:

private HttpCookie GetCookie()
{
    HttpCookie cookie = Request.Cookies["Cookie_SessionID"];
    if (cookie != null && cookie.Value != null)
    {            
        return cookie;
    }
    return null;        
}

由于某种原因,该cookie返回的getCookie()总是有一个期满 0001-01-01 00:00:00 ,即使当我在浏览器中查看饼干它具有正确的到期时间。

For some reason the cookie returned by GetCookie() always has an Expires value of 0001-01-01 00:00:00, even though when I view cookies in the browser it has the correct expiry time.

看了<一href="http://msdn.microsoft.com/en-us/library/aa289495%28v=vs.71%29.aspx#vbtchaspnetcookies101anchor5"相对=nofollow>这个其中规定过期的饼干根本就没有发送到服务器,我想这可能是发生的是,cookie被的的正确,但浏览器不是发送的到期日,因为它实际上是不必要的?...

Having read this which states expired cookies are simply not sent to the server, I assume what could be happening is that the cookie is being written correctly but the browser is not sending the expiry date because it's actually unnecessary?...

我的问题是,我想pcisely捕捉$ P $是 - 该Cookie已过期,所以他们不得不再次登录 - 但是我需要显示沿我知道你已经登录了线的消息在但你需要再做一次式的东西。

My problem is that I want to capture precisely that - the cookie has 'expired' and so they have to log in again - but I need to display a message along the lines of "I know you have already logged in but you'll need to do it again" type thing.

感谢

推荐答案

除了cookie的名称和值将不会发送任何服务器的浏览器。所有的其他属性(到期,域,路径,仅Http,...)不能检索上的cookie已被设定后的请求。

The browser will not send anything to the server except the cookie name and value. All of the other properties (expires, domain, path, httponly, ...) cannot be retrieved on requests after the cookie has been set.

如果要显示这样的信息,那么你将需要检测到用户登录了一些其他的机制,你可以设置一个presence饼干了一年左右,检查是​​否存在。

If you want to display such a message then you will need some other mechanism of detecting that the user was logged in. You might set a presence cookie for a year or so and check to see if it exists.

更被接受的方式来处理,这是当他们试图访问受保护的资源,并显示沿你需要登录后才能查看此页面的线条一些消息,将用户重定向到一个登录页面。如果你是pviously登录$ P $,您的会话可能已过期。

The more accepted way to deal with this is to redirect the user to a login page when they try to access a protected resource and display some message along the lines of "You need to log in to view this page. If you were previously logged in, your session may have expired."

(另请注意,您应该重新设定在每次请求的饼干,这样用户将不会被记录,如果他们继续使用该网站。目前尚不清楚,从你的code不管你是做这个还是不行。)

(Also note that you should be re-setting the cookie on every request, so that the user will not be logged out if they continue to use the site. It's not clear from your code whether you are doing this or not.)

这篇关于C#中的Cookie - Expires属性将不会设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆