更改ASP.NET的会话状态Cookie的到期 [英] Changing expiry on ASP.NET's Session State cookie

查看:223
本文介绍了更改ASP.NET的会话状态Cookie的到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET会话状态来跟踪我的网站上登录的用户。

I'm using ASP.NET Session State to keep track of logged in users on my site.

但是,我遇到的一个问题是,默认ASP.NET会话Cookie设置为在浏览器关闭时过期。

However, one problem I'm running into is that by default ASP.NET session cookies are set to expire when the browser closes.

我尝试设置自己的ASP.NET_SessionId cookie,并使用类似于以下代码修改cookie的到期日期:

I've tried setting my own ASP.NET_SessionId cookie and modifying the cookie's expiry using something similar to the following code:

Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(1);

这些方法都无效,他们都设置了第二个相同名称的cookie。

None of these approaches work, they all set a second cookie with the same name.

是否有改变会话cookie过期的方法?

Is there a way of changing the session cookie's expiry?

推荐答案

您使用FormsAuthentication跟踪登录的用户。您可以使用持久的FormsAuthenticationCookie来实现您想要的功能。

I would suggest you use FormsAuthentication to track logged in users. You can use a persistent FormsAuthenticationCookie to achieve what you want.

或者,如果您真的想使用会话状态,请尝试这种技巧

Or if you really want to use Session State, try this technique.

这篇关于更改ASP.NET的会话状态Cookie的到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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