2014-01-22 48 views
0

我在ASP.NET MVC上遇到了一个问题。 我已经创建了一个自定义的AuthorizeAttribute,以便从数据库中获取每个动作和每个控制器的角色。授权属性和缓存问题

下面是代码:

public class CustomAuthAttribute : AuthorizeAttribute 
{ 
    private string[] _roles; 
    private Db_Entities db = new Db_Entities(); 
    private String controller; 
    private String action; 

    public CustomAuthAttribute(String controller, String action) 
    { 
     this.controller = controller; 
     this.action = action;   
    } 


    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (httpContext == null) 
     { 
      throw new ArgumentNullException("httpContext"); 
     } 

     Actions myAction = db.Actions.Where(a => a.Libelle.Equals(this.action)).Single(a => a.Controller.Equals(this.controller)); 
     List<String> myGroupeList = new List<String>(); 
     foreach (Groupes g in myAction.Groupes) 
     { 
      myGroupeList.Add(g.Groupe); 
     } 
     this._roles = myGroupeList.ToArray<string>(); 

     IPrincipal user = httpContext.User; 
     if (!user.Identity.IsAuthenticated) 
     { 
      return false; 
     } 

     bool isAuthorized = false; 

     RoleProviderAD rp = new RoleProviderAD(); 
     string[] DbRoles = rp.GetRolesForUser(httpContext.User.Identity.Name); 
     foreach (string str in DbRoles) 
     { 
      if (this._roles.Contains(str)) 
      { 
       isAuthorized = true; 
      } 
     } 
     return isAuthorized; 
    } 

} 

当从“家”控制器“指数”动作被调用,AuthorizeCore被称为太以及有关用户权限的数据都是从数据库中捕获。 但是,如果我从数据库中更改数据,当我再次调用此页时,尽管代码似乎必须再次运行,但旧数据仍会被捕获。

我试图使用ActionFilterAttribute,但由于顺序,它不工作。我也尝试在我们的IIS服务器上添加一些规则。一切都失败了。 如果有人能帮助我,那会很棒!

回答

0

如果您正在使用的任何阿贾克斯calls.Try包括Ajax调用

$.ajaxSetup({ cache: false }); 
+0

没有Ajax调用这个动作 –

+0

你怎么打电话给你的方法之前,这个代码? – Sajeev

+0

在这种情况下,它是一个简单的链接:Html.ActionLink(“Accueil”,“Index”,“Home”) –