我创建了以下动作过滤器类:-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CheckUserPermissionsAttribute : ActionFilterAttribute
{
Repository repository = new Repository();
public string Model { get; set; }
public string Action { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string ADusername = filterContext.HttpContext.User.Identity.Name.Substring(filterContext.HttpContext.User.Identity.Name.IndexOf("\\") + 1);
if (!repository.can(ADusername,Model,Action))
{
filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");
}
base.OnActionExecuting(filterContext);
}
}
它将调用以下模型存储库类:-
public bool can(string user, string Model, string Action) {
if (Model == "Admin")
{
bool isadminByuser = tms.SecurityRoles.Where(a => a.Name == "Administrator").SingleOrDefault().SecurityRoleUsers.Any(a => a.UserName.ToLower() == user.ToLower());
var adminByGroup = tms.SecurityRoles.Where(a => a.Name == "Administrator").SingleOrDefault().Groups.Select(a2 => a2.TMSUserGroups.Where(a3 => a3.UserName.ToLower() == user.ToLower()));
bool isadminByGroup = adminByGroup.Count() >= 1;
if (isadminByGroup || isadminByuser)
{
return true;
}
}
return false;
}
以上将正常工作,但如果我更改数据库值,那么存储库值;isadminByuser & adminByGroup,会有相同的值(缓存值)。但是如果我停止该项目并从 Visual Studio 重新构建并再次运行该项目,我将获得正确的值。如果我有缓存问题,任何人都可以提出建议,除非重新运行项目,否则强制存储库具有相同的值?
谢谢