![](/img/trans.png)
[英]Authorization issue when use [Authorize] attribute in page in client blazor app
[英]Issue with Authorize attribute and cache
我在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服務器上添加一些規則。 一切都失敗了。 如果有人可以幫助我,那就太好了!
如果您正在使用任何ajax調用,請嘗試在ajax調用之前包含此代碼
$.ajaxSetup({ cache: false });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.