2009-09-24 42 views
2

下面我有我的Global.asax Session_Start.NET会话状态已创建一个会话ID,但无法保存它,因为反应是由应用程序

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) 

这只是发生时,我尝试通过(例如)谷歌转码器来访问它,但我想知道为什么它发生,我怎么能阻止它。

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

+0

顺便说一句:你好像有什么在做(防止某些浏览器访问一个网页)会在大多数情况下认为是不好的做法您可能依赖于客户端发送的User-Agent字符串,该字符串很容易被欺骗。如果您只是想警告用户有关潜在的不兼容问题,这将是合理的。但是从你使用的语言(“禁止浏览器”),我认为这不仅仅是一个不兼容的警告。 当然,你可能已经知道这一点,并有你的理由。只是想确保你牢记这些陷阱。 –

+0

这是一个移动网站,有时来自谷歌搜索的用户通过谷歌的网页代码转换器来移除它,从而删除所有内容并杀死用户经验。 有些时候人们/成员使用gogole代码转换器。 – nLL

+0

答案在这里:http://stackoverflow.com/questions/904952/whats-causing-session-state-has-created-a-session-id-but-cannot-save-it-becau/1966562#1966562 – Adi

回答

2

您是否尝试翻转放弃会话与重定向的顺序?

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

我已经这样做了,但是需要时间才能看到结果,因为我不会在每个代码转换器请求上出现错误。 – nLL

+0

@JustLoren - 有完全相同的问题和您的解决方案解决它;-)谢谢。 – MaYaN

4

你见过this thread

我遇到了这个例外(在不同的上下文中),并与下面的固定它...

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

如何将sessionid分配给一个字符串会帮助我?一旦我知道它是一个转码器,我不需要会话ID。我不想分配会话ID一旦它被刷新,因为我需要检查下一个请求,如果它是代码转换器。 – nLL

+0

假设'SessionID' get访问器有一些副作用行为,导致会话ID在被访问时被初始化。 'Session_Start'事件可能在页面生命周期中足够早,以至于在此之后的某个时刻会话ID被安全地保存,并且不会在OP中产生错误。所以重点不在于获取会话ID字符串值,而是关于简单地使'SessionID'获取访问器运行。 – JLRishe

相关问题