将敏感数据放入缓存中可以吗?ASP MVC

卡洛斯·米格尔·科兰塔(Carlos Miguel Colanta)

我目前为我的MVC应用程序实现了一些“检查器”。

到目前为止,这就是我所拥有的,

  1. 授权书(表格)
  2. 身份验证(自定义RoleProvider)
  3. 动作过滤器(以确保用户不会放置任何虚假的ID号或尝试通过编辑GETurl来访问其他人的数据

cache关于ASP MVC的最佳做法,我有几个问题

这是我登录的实现:

  [HttpGet]
    [ActionName("login")]
    public ActionResult login_load()
    {
        return View();
    }

    [HttpPost]
    [ActionName("login")]
    public ActionResult login_post(string uname,string pword)
    {

        using (EmployeeContext emp = new EmployeeContext())
        {
           //h student log = new student();
            int success = emp.login.Where(x => x.username == uname && x.password == pword).Count();
            if (success == 1)
            {
                int id = (from logs in emp.login
                          join rol in emp.roles on logs.role equals rol.id
                          where logs.username == uname
                          select logs.id).First();

                FormsAuthentication.SetAuthCookie(uname, false);
                HttpRuntime.Cache.Insert("id", id);
                return RedirectToAction("Details", "Enrollment", new { id = id});
            }
            return View();
        }
    }

(我计划尽快实施H&S)

无论如何,到目前为止,这是我的担忧:

  1. 出于安全方面的考虑,可以id在缓存中存储诸如的内容吗?或者,如果我使用更好sessions
  2. 假设我成功登录,并添加了以下代码行:

    HttpRuntime.Cache.Insert("id", id);

    是要编辑我以前的记录还是要添加另一个条目?我有来自Custom的这段代码RoleProviderHttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration);并且我相信每次我问保护为的控制器时,它们都会被“解雇” [Authorize(Role="users")]那么,它是否要创建一个新条目或编辑上一个/现有条目?

  3. 我应该担心cache用户决定注销后立即删除/清除我的信息吗?我的角色提供者超时当前设置为20 minutes

我需要使用,id因为除了用户名之外,它是我的唯一标识符,并且我使用它来比较用户尝试访问的任何ID。

我在想是否有可能编辑缓存并针对我的应用程序使用它。

夜猫子888

出于安全方面的考虑,可以在缓存中存储诸如id之类的东西吗?还是最好使用会话?

在这种情况下,差别不大。但是,缓存不能分布在多个Web服务器上。会话状态可以通过更改文件<sessionState>部分来实现web.config因此,您通过使用缓存在应用程序的可伸缩性方面建立了固有的限制。

假设我成功登录,并添加了以下代码行:

HttpRuntime.Cache.Insert("id", id);

是要编辑我以前的记录还是要添加另一个条目?我从我的自定义角色提供程序HttpRuntime.Cache.Insert(cacheKey,Roles,null,DateTime.Now.AddMinutes(_cacheTimeoutInMinute),Cache.NoSlidingExpiration)中获得了这些代码; 并且我相信每次我问一个具有[Authorize(Role =“ users”)]保护的控制器时,它们都会被“解雇”。那么,它是否要创建一个新条目或编辑上一个/现有条目?

首先,您的代码存在一个重大缺陷。缓存旨在在站点上的所有用户之间共享。因此,当您插入一个值(例如)时HttpRuntime.Cache.Insert("id", id);,所有用户都会看到它。如果您以后使用此值查找数据,则用户数据将始终是最后登录的用户的数据。

您可以通过在密钥中添加用户唯一的值来解决此问题。

var key = this.User.Identity.Name + "|Id";
HttpRuntime.Cache.Insert(key, id);

注意我在这里使用竖线字符作为分隔符。这是假设用户名不允许使用竖线字符(您还需要确保该字符)。

其次,使用适当的缓存模式意味着您将不必担心“ id”是否存在,因为您已经进行了该检查。使用缓存通常看起来像这样。

public static string GetUserID()
{
    // Check whether the user is logged in
    if (!HttpContext.Current.User.Identity.IsAuthenticated) {
        return 0;
    }

    // Make a UNIQUE key that can be used for this scenario
    var userName = HttpContext.Current.User.Identity.Name;
    var key = userName + "|Id";

    // Attempt to get the ID from the cache
    var id = HttpRuntime.Cache[key];

    // A null value indicates there was no value in the cache
    if (id == null)
    {
        // No ID in the cache, look it up from the database
        using (EmployeeContext emp = new EmployeeContext())
        {
            id = (from user in emp.login
                where user.username = userName
                select user.id).First();
        }
    
        // Store the ID from the database into the cache
        HttpRuntime.Cache.Insert(key, id, 
        
            // No Dependencies
            null, 
            
            // No absolute expiration (mimic the behavior of forms authentication)
            System.Web.Caching.Cache.NoAbsoluteExpiration, 
            
            // Timeout 20 minutes after the last access 
            // (to mimic the behavior of forms authentication)
            new TimeSpan(0, 20, 0),

            // Setting to NotRemovable ensures that if the
            // application pool restarts, you don't lose your cache
            System.Web.Caching.CacheItemPriority.NotRemovable, 

            // No callback needed here
            null);
    }

    return (string)id
}

当然,如果该值可在登录时将其直接插入到高速缓存中,则可以提高性能,但是在这种情况下,您需要确保使用相同的键。

在这种情况下,会话可能是一个更好的选择,但是无论哪种方式,您都应该利用此模式来仔细检查您是否有一个值,然后再将其返回给用户。

用户决定注销后,我是否应该担心删除/清除缓存?我的角色提供者超时当前设置为20分钟

如果您使用会话状态而不是缓存,这会容易得多。Session.Abandon()用户注销后只需致电即可

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Win 7/8中可以安全地打印敏感数据,还是可以将纯文本缓存到磁盘?

来自分类Dev

检查敏感数据时可以使用PHP中的标头重定向吗

来自分类Dev

MVC中的缓存位置

来自分类Dev

MVC中的缓存位置

来自分类Dev

在MVC中解密数据吗?

来自分类Dev

在ASPNET MVC SinglePageApplication中缓存数据

来自分类Dev

如何使用缓存对象缓存ASP.NET MVC数据

来自分类Dev

在ASP.NET MVC中以不可缓存的形式进行缓存

来自分类Dev

将敏感数据保存在Electron中

来自分类Dev

ASP.Net MVC ActionResult返回缓存的视图吗?

来自分类Dev

是否可以在ASP.NET MVC和Identity 2.0中缓存授权?

来自分类Dev

Asp .Net MVC,如何将值放入/包含 DropDownListFor?

来自分类Dev

我们可以将ViewModel分配给ASP.Net MVC中的ViewData对象吗?

来自分类Dev

MVC Seed不会将数据放入数据库中

来自分类Dev

会话对敏感数据安全吗?

来自分类Dev

如果我不小心将敏感数据放入Github,该怎么办?

来自分类Dev

ASP.NET MVC 5数据缓存-行为异常

来自分类Dev

如何在ASP.NET MVC 4中的服务器上缓存数据?

来自分类Dev

在ASP.NET MVC 4中将缓存设置为最大级别

来自分类Dev

将数据存储在内存中,所有用户都可以访问asp.net mvc

来自分类Dev

在ASP .NET MVC中存储会话数据

来自分类Dev

在ASP .NET MVC中存储会话数据

来自分类Dev

ASP.NET MVC 中的数据绑定

来自分类Dev

在ASP.NET MVC中实现静态文件缓存

来自分类Dev

浏览器中的ASP.NET MVC图像缓存

来自分类Dev

清除MVC 6 / ASP.NET 5中的输出缓存

来自分类Dev

在ASP.NET MVC中实现静态文件缓存

来自分类Dev

浏览器中的ASP.NET MVC图像缓存

来自分类Dev

如何在ASP.NET 5 MVC中访问缓存?

Related 相关文章

热门标签

归档