Reputation: 4602
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.
Upvotes: 0
Views: 2946
Reputation: 8877
You could implement IHttpModule. A guide to implement one can be found here. The good thing about httpmodule is that you can catch almost any HTTP response and do what you want with it.
Or you can look at this SO question. Maybe you will like it better.
Upvotes: 0