如何使一个ASP.NET会话cookie的ASP.NET HttpSession的过期吗? [英] How to make an ASP.NET session cookie expire with the ASP.NET HttpSession?

查看:95
本文介绍了如何使一个ASP.NET会话cookie的ASP.NET HttpSession的过期吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经要共享一个子域的数据创建的HttpCookie:

I have created an HttpCookie in order to share data across a subdomain :

HttpCookie cookie = new HttpCookie("sessionGUID");
cookie.Value = value;
cookie.Domain = ".example.com";

Response.Cookies.Set(cookie);

我不小心假设,因为它是一个会话饼干(无到期),这将伴随着我的ASP.NET会话过期。当然,浏览器,这是一个'浏览器会话'cookie并没有联系ASP.NET会话。

I carelessly assumed since it is a 'session' cookie (no expires) that it would expire along with my ASP.NET session. Of course to the browser this is a 'browser session' cookie and not linked to the ASP.NET session.

我想要做的就是与ASP.NET会话过期沿着这些'浏览器会话'饼干。

What I want to do is expire these 'browser session' cookies along with the ASP.NET session.

我想我只需要设置为过期值相同的ASP.NET会话。我将如何编程确定呢?

i think I just need to set Expires to the same value as the ASP.NET session. How would i determine this programatically?

推荐答案

如果他们只需要靠近则是这样的:

If they just need to be close then something like this:

int expirationMinutes = Session.Timeout;
if (System.Web.HttpContext.Current.Response.Cookies["monster"]!=null)
{
    System.Web.HttpContext.Current.Response.Cookies["monster"].Expires =
                                 DateTime.Now.AddMinutes(expirationMinutes);
}

会话计时器重置所有用户职位或得到,这样以来code是在服务器上,现在在本届会议上执行,剩余的时间就是查看Session.Timeout,说20分钟。

The session timer resets everything the user posts or gets, so since code is executing on the server right now, the remaining time on the current session is Session.Timeout, say 20 minutes.

在20分钟后,该cookie将不被接受,你就会有发布一个新的。

In 20 minutes, the cookie will not be accepted and you'll have to issue a new one.

这篇关于如何使一个ASP.NET会话cookie的ASP.NET HttpSession的过期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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