我在asp.net mvc-4 Web应用程序中使用自定义角色提供程序。
在我的'CustomRoleProvider'类中,我扩展了'RoleProvider'接口,并且我在类中覆盖'IsUserInRole','GetRolesForUser'和'GetAllRoles'函数。这很好。
现在我正在尝试重定向自定义页面(例如:“〜/ Security / AccessDenied / Index”),如果用户尝试访问用户无法访问的操作。如果用户在默认情况下尝试这样做,则会重定向到主页。为了重定向我的自定义页面,我扩展了'AuthorizeAttribute'界面,并且我覆盖了'OnAuthorization'功能。但看起来像'OnAuthorization'函数永远不会被调用。
这是扩展'AuthorizeAttribute'界面的代码:
public class AccessDeniedAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectResult("~/Security/AccessDenied/Index");
}
}
}
为什么没有调用'OnAuthorization'功能?需要帮助......
答案 0 :(得分:2)
您需要将自定义属性应用于操作或控制器(或将其添加为所有请求的常规过滤器)。使用自定义属性而不是“授权”属性。
[AccessDeniedAuthorize(Roles="Admin")]
应该有用。