2016-08-03 124 views
0

我有需要全球權限的管理儀表板項目,我將它安放使用AllowAnonymous屬性使授權忽略

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new AuthorizeAttribute()); 
    } 

這些代碼讓我所有的控制器正在授權..還有就是有一個控制器[使用AllowAnonymous]屬性。但是我有突然變化的要求,這個控制器上的行動必須是授權..

[AllowAnonymous] 
public class AuthController : Controller 
{ 
    [Authorize(Roles = "Admin")] 
    public ActionResult BumbaSection() 
    { 
     return View(); 
    } 
} 

這不是工作,我仍然可以訪問這個BumbaSection行動。任何想法? 感謝

+0

首先,您不需要在全局過濾器中註冊AuthorizeAttribute,它已經是mvc框架的一部分。您如何進行身份驗證,以及如何儲存您的角色,向我們展示您的代碼。 –

回答

1

後,我偷看了授權代碼,這部分代碼進行授權不工作:

public virtual void OnAuthorization(AuthorizationContext filterContext) 
{ 
    //code here 

    if (filterContext.ActionDescriptor.IsDefined(typeof (AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof (AllowAnonymousAttribute), true)) 
     return; 

    //code here  
} 

覆蓋這一塊的授權屬性類的代碼和我的代碼開始工作..也許這將對我有問題的人有用