Jay
Jay

Reputation: 2703

Intercept Session Start event for all applications

I have an IIS server (7.5) that hosts several applications each of them run in their own application pool identity. I am trying to write some code that intercepts the Session On Start event. I have successfully written other IHttpModules that are processed for all requests but, in this case I only want to intercept the first time the session is initiated. I want to do this for all the web applications at a global level within my web site. My plan is to use this to capture the last logon date for the user on a per web app basis to satisify an auditing requirement.

I have all the pieces in place except the event that I need to intercept. It seems all IHttpModule Events fire on all requests. I figured the Session_Start event would be ideal but it doesn't look like I can tie into this from an IHttpModule.

I looked at the SessionStateUtility but I do not want to rewrite session functionality, I just want to intercept the start event.

Is there another interface out there I can use to intercept Session_Start? Any other recommendations?

Upvotes: 0

Views: 212

Answers (1)

Pankaj Kapare
Pankaj Kapare

Reputation: 7812

Have you tried something like this?

    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.
    }

Upvotes: 1

Related Questions