2016-08-03 80 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  
} 

覆盖这一块的授权属性类的代码和我的代码开始工作..也许这将对我有问题的人有用