Custom 401 error pages in MVC alongside Windows Authentication

Custom 401 error pages in MVC alongside Windows Authentication

We use windows authentication for a lot of our web apps, but also want to serve up a custom 401 error page when users are not authorized to log in. This is tricky because this specific 401 error is handled directly by IIS, rather than the application itself, because it’s a windows directory permission. I spent quite a bit of time searching for an elegant solution to this, but really came up with nothing. This is one way I’ve found to circumvent the issue, and serve up a custom 401, while still preserving your MVC application level error handling:

1) You will need an entry for your 401 error page in the httpErrors section of web.config:


<httpErrors errorMode="Custom">

	<remove statusCode="401" />

	<error statusCode="401" path="Views\Error\401.html" responseMode="File" />

</httpErrors>

2) You will then need to go into IIS and enable anonymous authentication AND windows authentication for your content folder, or whatever folder houses all your images and css used on your error page and throughout the rest of your application. I could not find a way to successfully enable this through the application’s web.config solely.

iis auth

This will now allow you to show a custom 401 error page, with images and css, to an unauthorized user. Once the user is authenticated successfully, they will still see the expected images and css.

If there is a better way to achieve this functionality, that I’m missing, please leave a comment and let me know, thanks!

There are 2 comments .

Greg K —

Is there a way to redirect using the Error.cshtml instead of a static file?

Reply »

Share Your Thoughts!

Testimonials

What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!

Dan

I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at brent@kruegerwebdesign.com or drop me a line using the button below:

Copyright © 2017 Krueger Web Design - All rights reserved
Web Design in Madison, WI