Cookie在应用程序日志中添加Global.asax警告

时间:2010-05-25 16:30:18

标签: c# asp.net warnings global-asax

在我的Global.ASAX文件中,我有以下内容:

void Session_End(object sender, EventArgs e) 
{
    System.Web.HttpCookie isAccess = new System.Web.HttpCookie("IsAccess");
    isAccess.Expires = DateTime.Now.AddDays(-1);
    isAccess.Value = "";
    System.Web.HttpContext.Current.Response.Cookies.Add(isAccess);
}

因此,每次在应用程序中调用此方法时,应用程序日志中都会记录以下事件作为警告:

Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 5/25/2010 12:23:20 PM 
Event time (UTC): 5/25/2010 4:23:20 PM 
Event ID: c515e27a28474eab8d99720c3f5a8e90 
Event sequence: 4148 
Event occurrence: 332 
Event detail code: 0 

Application information: 
    Application domain: /LM/W3SVC/2100509645/Root-1-129192259222289896 
    Trust level: Full 
    Application Virtual Path: / 
    Application Path: <PathRemoved>\www\ 
    Machine name: TIPPER 

Process information: 
    Process ID: 6936 
    Process name: w3wp.exe 
    Account name: NT AUTHORITY\NETWORK SERVICE 

Exception information: 
    Exception type: NullReferenceException 
    Exception message: Object reference not set to an instance of an object. 

Request information: 
    Request URL:  
    Request path:  
    User host address:  
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: NT AUTHORITY\NETWORK SERVICE 

Thread information: 
    Thread ID: 7 
    Thread account name: NT AUTHORITY\NETWORK SERVICE 
    Is impersonating: False 
    Stack trace:    at ASP.global_asax.Session_End(Object sender, EventArgs e) in <PathRemoved>\Global.asax:line 113

知道为什么这段代码会导致这个错误?

3 个答案:

答案 0 :(得分:2)

看起来您正在尝试在Session_End事件期间修改Cookie。这不起作用,因为会话结束(Session_End)不对应于来自用户的HTTP请求。相反,它是在从特定用户收到最后一个HTTP请求后20分钟(或任何会话超时设置为)启动的。

答案 1 :(得分:0)

  

知道为什么这段代码会导致   这个错误?

我的猜测是您在没有响应的方法中访问响应。

您使用Global.asax的哪种方法调用此代码?

答案 2 :(得分:0)

看起来像Session_End ......可能没有HttpContext,更不用说响应了。

如果你只是想在会话结束时清除一个cookie,你不能只设置一个过期日期,并在用户关闭浏览器时让它清楚吗?