【发布时间】:2014-04-15 15:33:44
【问题描述】:
我有一个 MVC 应用程序,它使用声明授权,基于可用的优秀教程 here。在代码中,我覆盖了 ClaimsAuthorizationManager 的 CheckAccess 方法,以针对每个资源和操作实现我自己的逻辑
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;
}
}
除了 CheckAccess 方法返回 false 时,我得到 HTTP 错误 401.0 – Unauthorized 之外,一切正常,正如我所读到的,它只能由 IIS 处理。我的问题是,如何在代码中处理此错误以向用户显示自定义错误页面。我见过许多不同的解决方案,例如 here 或 here,但从未设法使其适合我。
【问题讨论】:
标签: asp.net-mvc error-handling authorization claims-based-identity claims