【问题标题】:.net Session state has created a session id, but cannot save it because the response was already flushed by the application.net 会话状态已创建会话 ID,但无法保存它,因为响应已被应用程序刷新
【发布时间】:2009-09-24 16:37:07
【问题描述】:

我的 global.asax 中有以下Session_Start

string myBrowser = Utils.SafeUserAgent(Request);
foreach (string bannedbrowser in BrowserBan.BrowsersToBan)
{
   if (myBrowser.IndexOf(bannedbrowser, StringComparison.OrdinalIgnoreCase) > -1)
   {
       HttpContext.Current.Response.Redirect("/bannedBrowser.htm");
       Session.Abandon();
       break;
   }

它可以防止代码转换器访问我的网站。但时不时我会收到一个错误提示

System.Web.HttpException: Session state has created a session id, but cannot
  save it because the response was already flushed by the application.
    at System.Web.SessionState.SessionIDManager.SaveSessionID(
      HttpContext context, String id, Boolean& redirected, Boolean& cookieAdded)
    at System.Web.SessionState.SessionStateModule.CreateSessionId()
    at System.Web.SessionState.SessionStateModule.DelayedGetSessionId()
    at System.Web.SessionState.SessionStateModule.ReleaseStateGetSessionID()
    at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source,
      EventArgs eventArgs)
    at System.Web.SessionState.SessionStateModule.OnEndRequest(Object source,
      EventArgs eventArgs)
    at System.Web.HttpApplication.SyncEventExecutionStep.System.Web
      .HttpApplication.IExecutionStep.Execute()
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
      Boolean& completedSynchronously)

只有当我尝试通过(例如)Google Transcoder 访问它时才会发生这种情况,但我想了解它为什么会发生以及如何防止它发生。

我必须放弃会话,以便在刷新用户代理时重新评估。

【问题讨论】:

  • 顺便说一句:在大多数情况下,您似乎正在做的事情(阻止某些浏览器访问页面)将被视为不好的做法。您可能依赖于客户端发送的 User-Agent 字符串,这很容易被欺骗。如果您只是想警告用户潜在的不兼容问题,这是合理的。但是从您使用的语言(“禁止浏览器”)来看,我认为这不仅仅是一个不兼容警告。当然,您可能已经知道这一点并且有这样做的理由。只是想确保您牢记这些陷阱。
  • 它是一个移动网站,有时来自谷歌搜索的用户通过谷歌的网络转码器访问它,该转码器会删除所有内容并扼杀用户体验。有时人们/成员使用 gogole 转码器作为代理。

标签: c# global-asax


【解决方案1】:

你见过this thread吗?

我遇到了这个异常(在不同的上下文中)并用以下方法修复了它......

void Session_Start(object sender, EventArgs e) 
{
string id = Session.SessionID;
}

【讨论】:

  • 将 sessionid 分配给字符串对我有什么帮助?一旦我知道它是转码器,我就不需要会话 ID。我不想在刷新后分配会话 ID,因为我需要检查下一个请求是否是转码器。
  • 据说,SessionID get 访问器有一些副作用行为,会导致会话 ID 在被访问时被初始化。 Session_Start 事件可能在页面生命周期中足够早,以至于会话 ID 在之后的某个时间被安全保存,并且不会在 OP 中产生错误。所以重点不在于获取会话 ID 字符串值,而在于简单地使SessionID get 访问器运行。
【解决方案2】:

您是否尝试过颠倒放弃会话与重定向的顺序?

Session.Abandon();
HttpContext.Current.Response.Redirect("/bannedBrowser.htm", true);

【讨论】:

  • 我已经这样做了,但是我需要一些时间才能看到结果,因为我不会在每个转码器请求上都收到错误
  • @JustLoren - 遇到了完全相同的问题,您的解决方案解决了它;-) 谢谢。
猜你喜欢
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2022-06-10
  • 2017-01-10
相关资源
最近更新 更多