I have an MVC application, which uses claims authorization, based on great tutorial available here. In the code I override CheckAccess method of ClaimsAuthorizationManager to implement my own logic against each resource and action
public class CustomAuthorisationManager : ClaimsAuthorizationManager
{
public override bool CheckAccess(AuthorizationContext context)
{
string resource = context.Resource.First().Value;
string action = context.Action.First().Value;
if (action == "Show" && resource == "Code")
{
bool livesInScotland = context.Principal.HasClaim(ClaimTypes.Country, "Scotland");
return livesInScotland;
}
return false;
}
}
And everything works fine, apart of the fact that whenever CheckAccess method returns false I get HTTP Error 401.0 – Unauthorized, which as I have read can be handled only by IIS. My question is, how can I handle this error in code to display custom error page to the user. I have seen many different solutions, like here, or here, but have never managed to make it work for me.