Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 865
Anders Abel Avatar asked Nov 11 '22 23:11

Anders Abel


1 Answers

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)

like image 95
Lars Anundskås Avatar answered Nov 15 '22 11:11

Lars Anundskås