在mvc2应用程序中使用角色提供程序

时间:2010-12-14 16:07:09

标签: asp.net-mvc

我的函数名为带有属性的注释[Authorize(Roles =“Admin,Users”)]。 我有编辑注释按钮。在编辑功能中,我们有属性[Authorize(Roles =“Admin”)]。当用户尝试访问时我想抛出一个自定义错误页面。我没有使用表单身份验证只是角色提供程序。如何重定向到自定义错误页面。

1 个答案:

答案 0 :(得分:0)

编写自定义属性

例如

public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        private string[] UnAuthorizedRoles = new string[] { "USER" };

        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            bool authorized = true;
            foreach (string role in UnAuthorizedRoles)
            {
                if (httpContext.User.IsInRole(role))
                {
                    authorized = false;
                }
            }
            return authorized;
        }


        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
           // send to custom error page
        }
    }
相关问题