路径的设置和过期在asp.net会话cookie [英] Setting Path and Expiration for session cookie in asp.net

查看:244
本文介绍了路径的设置和过期在asp.net会话cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么我都试过没有工作。 Currenly我有以下code键更改asp.net会话cookie过期日期和路径,但ASP不想要听我的。我在发送的Set-Cookie头相同的cookie有时两次,有时它会发送它的默认Cookie路径无视和到期日期,有时它发出的一切如预期,有时它不会发送的Set-Cookie的。我该怎么办。这驱使我坚果:(

我的code在Global.asax中

 保护无效Application_ preRequestHandlerExecute(对象发件人,EventArgs的发送)
{
  ///只适用于会话cookie持久性要求会话信息的请求
  如果(Context.Handler是IRequiresSessionState || Context.Handler是IReadOnlySessionState)
  {
    VAR的sessionState = ConfigurationManager.GetSection(System.Web程序/的sessionState)作为SessionStateSection;
    VAR cookieName = =的sessionState空&放大器;!&安培; !string.IsNullOrEmpty(sessionState.CookieName)
      ? sessionState.CookieName
      ASP.NET_SessionId    VAR超时=的sessionState!= NULL
      ? sessionState.Timeout
      :TimeSpan.FromMinutes(20);    ///确保ASP.NET会话cookie整个子域访问。
    如果(Request.Cookies时[cookieName] = NULL&放大器;!&安培;!会话= NULL&放大器;&安培;!Session.SessionID = NULL)
    {
      Response.Cookies [cookieName] .value的= Session.SessionID;
      Response.Cookies [cookieName]。路径= Request.ApplicationPath;
      Response.Cookies [cookieName] .Expires = DateTime.Now.Add(超时);
    }
  }
}


解决方案

也许如果您尝试使用系统调用,而不是直接改变饼干..

  HttpContext.Current.Session.Timeout = 20;

更新

您的意见后,从你对我说的,是你尝试使用相同的cookie,2不同的应用程序,并设置不同的超时。但是,asp.net可以只保留一个会话cookie的每一个。为了解决这个,你必须使用2个不同的命名饼干,而不是不同的Cookie路径。

在每个应用程序的目录,在web.config文件中,将

 <的sessionState cookieName =AppSessionCookieName..>

有关更多相关信息搜索参考
http://msdn.microsoft.com/en-us/library/h6bb9cz9.aspx

希望这会解决这个问题。

Anything I have tried didn't work. Currenly I have following code to change asp.net session cookie expiration date and path, but asp doesn't want to listen to me. I sends same cookie in Set-Cookie header two times sometimes, sometimes it sends it's default cookie ignoring path and expiration date, sometimes it sends everything as expected, and sometimes it doesn't send Set-Cookie at all. What should I do. This drives me nuts :(

My code in Global.asax

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
  /// only apply session cookie persistence to requests requiring session information
  if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
  {
    var sessionState = ConfigurationManager.GetSection("system.web/sessionState") as SessionStateSection;
    var cookieName = sessionState != null && !string.IsNullOrEmpty(sessionState.CookieName)
      ? sessionState.CookieName
      : "ASP.NET_SessionId";

    var timeout = sessionState != null
      ? sessionState.Timeout
      : TimeSpan.FromMinutes(20);

    /// Ensure ASP.NET Session Cookies are accessible throughout the subdomains.
    if (Request.Cookies[cookieName] != null && Session != null && Session.SessionID != null)
    {
      Response.Cookies[cookieName].Value = Session.SessionID;
      Response.Cookies[cookieName].Path = Request.ApplicationPath;
      Response.Cookies[cookieName].Expires = DateTime.Now.Add(timeout);
    }
  }
}

解决方案

Maybe if you try using the system call, and not direct change the cookies..

HttpContext.Current.Session.Timeout = 20;

update

After your comments, and from what you say to me, is that you try to use the same cookie, for 2 different applications, and set different time outs. But asp.net can keep only one session for every cookie. To solve that you must use 2 different named cookies, and not different cookie path.

On each application directory, on web.config, change the

<sessionState cookieName="AppSessionCookieName" ..>

Reference for more infos
http://msdn.microsoft.com/en-us/library/h6bb9cz9.aspx

Hope this solve the problem.

这篇关于路径的设置和过期在asp.net会话cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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