我有一个关于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服务器上添加一些规则。一切都失败了。如果有人能帮我,那就太好了!
发布于 2014-01-22 19:52:00
如果您正在使用任何ajax calls.Try在ajax调用之前包含此代码
$.ajaxSetup({ cache: false });https://stackoverflow.com/questions/21278699
复制相似问题