【问题标题】:Put data from database into Session in MVC将数据库中的数据放入MVC中的Session
【发布时间】:2013-06-16 16:24:00
【问题描述】:

我目前正在开发过滤器 [Authorize] 的扩展,以便我可以从数据库中检索权限。一切正常,但这肯定是一个性能问题。每次我向数据库发送查询请求权限时,这都不是确定这一点的最佳方法。所以我想把这些数据放在Session中。将数据库中的数据放入 Session 对象的最快方法是什么,我可以询问(LINQ)以及数据库。

现在看起来像:

var _allowedRolesDB = context.sec_RolesInCAs
                .Where(rl => rl.MenuControlName == controllRights && rl.MenuActionName == actionRights)
                .Select(rl => rl.RoleName);

            foreach (var r in _allowedRolesDB)
            {
                RolesDB = RolesDB + r.ToString() + ",";
            }

但我想改成

var _allowedRolesDB = MySuperSessionSomethink
.Where(rl => rl.MenuControlName == controllRights && rl.MenuActionName == actionRights)
                .Select(rl => rl.RoleName);

            foreach (var r in _allowedRolesDB)
            {
                RolesDB = RolesDB + r.ToString() + ",";
            }

MySuperSessionSomethink 将保留从数据库中一次性检索到的数据。知道我该怎么做吗?发送帮助。


更大的画面

好的。我将展示更大的画面。 整个想法是创建自定义授权过滤器。

    [CustomAuthAttribute("Home,Index", Roles = "SuperAdministrator")]
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }

这个目标是什么。创建具有所有优点的授权属性,以及将权限信息保存在数据库中等附加功能。

现在我要做的是:

public CustomAuthAttribute(params string[] controllerAction)
{
        IPrincipal user = HttpContext.Current.User;
        string userName = user.Identity.Name;
        **... some code .. and take all allowed roles and check it have permissions** 
        var _allowedRolesDB = context.sec_RolesInCAs
            .Where(rl => rl.MenuControlName == controllRights && rl.MenuActionName == actionRights)
            .Select(rl => rl.RoleName);

        foreach (var r in _allowedRolesDB)
        {
            RolesDB = RolesDB + r.ToString() + ",";
        }
        **... some code .. thesame withs single users**

}

之后我使用

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
   **... can acces or not part of code ...**
   if (_rolesSplit.Any(user.IsInRole))
   {
        return true;
    }
}

但是有问题。每次我向数据库询问权限时,恕我直言,这不是最好的方法。现在我的想法是为一个用户获取所有权限,并在他被授权时将他放入他的会话中。也许我错了,这种方式会产生问题,但是保留在数据库中并一直询问权限也不是一个好主意:)。那么,在对数据库提出一两个问题后,也许更好的方式来获取数据并在代码中使用?

【问题讨论】:

    标签: asp.net-mvc database entity-framework session authorize-attribute


    【解决方案1】:

    首先,您似乎希望将这些添加到缓存中,而不是会话中。它们对应用程序来说似乎是全局的,而不是特定于用户的。由于您正在通过菜单控件/操作查找角色,因此我只需将它们作为查找添加到缓存中:

    string action = controlRights + actionRights;
    string allowedRoles = Cache[action];
    if (allowedRoles == null) 
    {
        allowedRoles = String.Join(",", context.sec_RolesInCAs
            .Where(rl => rl.MenuControlName == controlRights && rl.MenuActionName == actionRights)
            .Select(rl => rl.RoleName)
            .ToArray());
        Cache[action] = allowedRoles;
    }
    

    这将为您提供给定控制/操作的允许角色,来自第二个请求的缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2021-04-13
      相关资源
      最近更新 更多