拦截所有应用程序的会话启动事件

Intercept Session Start event for all applications

我有一个 IIS 服务器 (7.5),它托管多个应用程序,每个应用程序 运行 在它们自己的应用程序池标识中。我正在尝试编写一些代码来拦截 Session On Start 事件。我已经成功编写了针对所有请求处理的其他 IHttpModule,但在这种情况下,我只想在第一次启动会话时进行拦截。我想在我的网站中对全球范围内的所有 Web 应用程序执行此操作。我的计划是使用它在每个 Web 应用程序的基础上捕获用户的最后登录日期以满足审核要求。

除了我需要拦截的事件外,我已经准备好了所有的部分。似乎所有 IHttpModule 事件都会在所有请求上触发。我认为 Session_Start 事件是理想的,但它看起来不像我可以从 IHttpModule 绑定到这个。

我查看了 SessionStateUtility 但我不想重写会话功能,我只想拦截启动事件。

有没有其他接口可以用来拦截 Session_Start?还有其他推荐吗?

你试过这样的东西吗?

    public void Init(HttpApplication context)
    {
        var sessionModule = context.Modules["Session"] as SessionStateModule;
        if (sessionModule != null)
        {
            sessionModule.Start += this.Session_Start;
        }
    }
    private void Session_Start(object sender, EventArgs e)
    {
        // Do whatever you want to do here.
    }