2010-12-14 87 views
2

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

回答

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 
     } 
    } 
相关问题