Anders Abel
Anders Abel

Reputation: 69260

Passing Error from HttpModule to MVC Application

I have a custom HttpModule for authentication. In the AuthenticateRequest event handler of the module I check if a custom SSO ticket is available and in that case perform some authentication logic.

If the SSO ticket is available, but incorrect, I would like to show a friendly error message to the user. I would prefer to be able to somehow set HTTP Status 401 (Unauthorized, used when authentication fails) and pass on a meaningful error message from the http module.

I would then like the MVC application to somehow get this information and use it to render a custom 401 error page which includes the meaningful error message from the module.

What's the best way to raise the error in the http module and what's the best way to have it display the error using the MVC application (to get the error message in the MVC application's layout)?

Upvotes: 5

Views: 287

Answers (1)

Lars Anundskås
Lars Anundskås

Reputation: 1355

You could create a custom controllerfactory, and in your:

protected override IController GetControllerInstance(RequestContext context, Type controllerType)

You will have access to the HttpContext and RouteData through your context object. If user is not authenticated you can manipulate your routedata to targed your desired controller/action with desired routeconstraints/parameters (for instance a http status message)

Upvotes: 1

Related Questions