I have implemented CustomAuthorization
overriding AuthorizeAttribute
.
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
var isUserExist = false;
if (isAuthorized)
{
//To check if user exists in DB
}
return isAuthorized && isUserExist;
}
}
This is working fine, when the user is not authorized, it is displaying 401
unauthorized error.
But on this case I want to redirect it to the Custom Unauthorized Page
. For this I have added the below in web.config
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="401" redirect="~/Home/Unauthorized" />
</customErrors>
In my controller,
[Authorize]
public class HomeController : BaseController
{
[AllowAnonymous]
public ActionResult Unauthorized()
{
return View(ActionMethods.HOME_UNAUTHORIZED);
}
}
My issue is, it is not redirecting to the custom page instead it is prompting for Username/password browser popup.
Sharing ideas/experiences will be better.
Update from Comments:
I am using Windows Authentication
and in IIS
Enabled both Windows Authentication
and Anonymous Authentication
.