2010-12-14 89 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 
     } 
    } 
相關問題