4

I'm creating an http module where I want to check if a request is coming from an authenticated user and redirect to the login page if it's not.

I registered the module in the web.config file and I have the following code that's throwing an exception:

public class IsAuthModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication TheApp)
    {
        var TheRequest = TheApp.Request;

    }
}

It throwing an exception that says "Request is not available in this context"

What am I doing wrong?

1

1 Answer 1

6

In the Init stage you have no request in progress. You have to subscribe the event for beginning of a request:

public void Init(HttpApplication TheApp)
{
    TheApp.BeginRequest += Application_BeginRequest;

    // End Request handler
    //application.EndRequest += Application_EndRequest;
}

private void Application_BeginRequest(Object source, EventArgs e) 
{
  // do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this nice concise answer, sure beats tutorial-length explanations!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.