在MVC中保持会话中的用户信息不安全

时间:2014-04-23 06:09:18

标签: asp.net-mvc security session-variables

我在博客上发布了关于SessionsCookies的帖子。这是细节

会话

  1. 会话更安全
  2. 会话在服务器上
  3. 缓存

    1. Cookie在客户端
    2. 不太安全
    3. 一旦在浏览器上禁用它就很难使用。
    4. 在上述论点的基础上,我使用了登录系统中的会话来保持UserId,UserName & roleName

      现在,在roleName的基础上,我将决定是否Admin进入管理员部分。

      我在MVC模型中使用了此代码

          public bool LoginMe()
              {
               Int64 Error;
                      //create db
                      Database db = DatabaseFactory.CreateDatabase("DBContext");
      
                      DbCommand dbCommand = db.GetStoredProcCommand("ValidateUser");
      
                      db.AddInParameter(dbCommand, "@Username", DbType.String, this.UserName);
                      db.AddInParameter(dbCommand, "@Password", DbType.String, EncryptPassword(this.Password));
                      db.AddOutParameter(dbCommand, "@Error", DbType.Int64, 10);
                      DataSet dsResult = db.ExecuteDataSet(dbCommand);
                      Error = Convert.ToInt64(db.GetParameterValue(dbCommand, "@Error"));
                     if (Error == 1100)
      {
          try
          {
              var query = (from o in dsResult.Tables[0].AsEnumerable()
                              select new AllUser
                              {
                                  UserId = o.Field<int>("UserId"),
                                  UserName = o.Field<string>("UserName"),
                                  roleName = o.Field<string>("roleName"),
                              }).Single(); // this will raise an exception if there isn't just one record returned
      
              Session["UserId"] = query.UserId;
              Session["UserName"] = query.UserName;
              Session["roleName"] = query.roleName;
      
              return true;
          }
          catch {
          // do nothing and let method return false as something has gone wrong.
          // add logging here if you are using it to show there has been a problem
          }
          }
          return false;
          }
      

      我在视图中使用它@Session["UserId"]

      现在专家对此发表评论

      If you aren't using https and securing the session cookie then this might make it easy to hack your site, although that's the same for any session based site (nearly all of them)
       It might be nice to add some check so that if you remove a user's rights, the session variables are deleted the next time that user requests something from the server, 
       otherwise they could carry on using the site even though their account it banned.You'd        have to decide if this is likely and then how you want to do this (using an authorization filter maybe.)
      

      以上评论让我感到困惑。任何人都能说清楚吗?保存这些信息的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

会话状态使用客户端票证来识别服务器端会话,它可能容易受到会话ID欺骗和注入攻击。

因此,要破解会话值,需要攻击远程服务器。

是的,对于高度安全的应用程序(例如网上银行),请使用https。

http://msdn.microsoft.com/en-us/magazine/cc163730.aspx#S9

应使用安全套接字层(SSL)来防止会话ID,身份验证票证,应用程序Cookie和其他请求/响应信息的网络级嗅探。

Can session value be hacked?

答案 1 :(得分:0)

如果您的应用程序处理敏感信息(信用卡号,帐号,密码),请使用HTTPS。 将User对象(带有userId,用户名,角色的模型)存储在会话中,而不是单独的属性 为SESSION_ID设置setHttpOnly属性。

在调用每个操作以反映存储在数据库中的当前权限之前,刷新会话中存储的User对象可能成本很高。