Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I've been facing this issue so long and tried almost everything but couldn't get what I want.

I want to display an custom error page for code 401 , if the user is not authorized to view any particular part of my webApplication. Kindly solve my problem.

What I have tried:

This is my web.config file:

<customErrors mode="On">
<error statusCode="404" redirect="~/Error/NotFound"/>
<error statusCode="401" redirect="~/Home/SecureMethod"/>

This is My Controller:

public class HomeController : Controller

public ActionResult SecureMethod()

return View();

public ActionResult NonSecureMethod()
return View();

I have also enabled the Windows Authentication and Anonymous Authentication on IIS Server
Updated 15-Aug-17 22:49pm
Namith Krishnan E 16-Aug-17 5:49am    
are you using windows authentication in your application or just enabled it in the iis for testing?
Rohit Singh 16-Aug-17 6:53am    
I'm using Windows authentication
Richard Deeming 17-Aug-17 11:34am    
Your "not authenticated" error is pointing to an action that requires authentication. Did you mean to point it to NonSecureMethod instead?

NB: 401 means the user hasn't authenticated yet. If you're trying to handle the case where the user has authenticated, but doesn't have permission, you need 403.
List of HTTP status codes[^]

1 solution

If 'Anonymous Authentication' is enabled every not-authenticated user will be mapped automatically to the user defined there, so there may be no error of authentication at all if your user behind 'Anonymous Authentication' has enough rights...
So if you want to users of your site to authenticate before getting access, turn 'Anonymous Authentication' off...
Share this answer
Rohit Singh 16-Aug-17 5:18am    
Hii, Kornfeld Eliyahu Peter I've tried this too but it dnt work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900