HttpModule无法设置Session

HttpModule无法设置Session

本文介绍了IIS HttpModule无法设置Session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用IHttpModule设置Session值。我已经将HttpModule设置为用于所有请求,而不仅仅是托管/ ASPX页面。



但是我发现,当请求非ASP.NET页面时进来(例如gif)HttpApplication的Session成员会抛出异常或者为null。



我发现以下SO帖子的原因是:





我是否需要做任何事情来强制保存会话?我假设在交换虚假的HttpHandler时初始化Session,不得不将原来没有实现IRequiresSessionState的原因导致会话在请求结束时不被保存。

解决方案

我刚刚解决了这个问题,我不得不将原始HttpHandler的重新映射从PostAcquireRequestState移到PreRequestHandlerExecute。


I am trying to set Session values using a IHttpModule. I have set the HttpModule to be used for all requests, not just managed/ASPX pages.

I found however, that when a request for a non-ASP.NET page came in (such as gif) the Session member of the HttpApplication would throw an exception or be null.

I found the following SO post for the reason behind this: link

When I am swapping out the HttpHandler with one that implements IRequiresSessionState the Session member is available in the HttpModule, and a Set-Cookie is sent back to the browser, but later when I try to read the contents of the session in a normal ASPX page none of the values I set are there. When debugging I can see that the SessionID is the same in both the HttpModule and normal ASPX page.

Do I need to do anything to force the session to be saved? I am assuming that while swapping in the fake HttpHandler initialises the Session, having to put the original which doesn't implement IRequiresSessionState back in causes the session to not be saved at the end of the request.

解决方案

I just worked it out, I had to move the re-mapping of the original HttpHandler from PostAcquireRequestState to PreRequestHandlerExecute.

这篇关于IIS HttpModule无法设置Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:07