.net 会话状态已创建会话 ID,但无法保存它,因为响应已被应用程序刷新

发布于 2024-08-05 22:12:45 字数 1523 浏览 4 评论 0原文

我的 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 访问它时才会发生这种情况,但我想了解为什么会发生这种情况以及如何防止它发生。

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

I have below in my 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;
   }

It prevents transcoders accessing o my site. but every now and then i get an error saying

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)

this only happens when I try to access it via (for example) Google Transcoder but I want to understand why it happens and how can I prevent it.

I have to abandon session so that on refresh user agent would re-evaluated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2024-08-12 22:12:45

您看过此帖子吗?

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

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

Have you seen this thread?

I encountered this exception (in a different context) and fixed it with the following ...

void Session_Start(object sender, EventArgs e) 
{
string id = Session.SessionID;
}
一个人练习一个人 2024-08-12 22:12:45

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

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

Have you tried flipping the order of abandoning your session vs redirecting?

Session.Abandon();
HttpContext.Current.Response.Redirect("/bannedBrowser.htm", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文