【问题标题】:SQL Server Object Explorer password is visible [closed]SQL Server对象资源管理器密码可见[关闭]
【发布时间】:2017-05-18 14:12:27
【问题描述】:

如何使密码在 SQL Server 对象资源管理器中不可见? (Visual Studio 2013)。我搜索了很多,但没有找到。请帮忙

【问题讨论】:

  • 以纯文本形式存储密码是个坏主意。看看他的question,其中包含关于密码最佳实践的精彩讨论。
  • 服务器资源管理器显示您在该字段中输入的内容。如果您不希望它可理解,那么您的程序应该加密该文本
  • 最好的方法是不在数据库中存储纯文本密码。散列和加盐您的密码,然后存储结果。或者,使用任意数量的身份验证库之一。
  • SQL server 数据库可以有一个 SQL 密码或一个 windows 密码,使用 windows 中的用户登录密码。使用 Windows 密码将无需在应用程序中输入密码。
  • 您无法阻止客户端显示他们从数据库返回的数据,您只能通过使数据不可读但仍然可用来阻止他们实际获取“可读”数据。对于密码,以明文形式存储它们完全是错误的,绝对不能这样做!,这一点我怎么强调都不为过。您需要使用加盐和散列,这也需要更改您的应用程序逻辑。 您必须这样做!。网上有很多关于如何正确保护密码存储的文章,我建议你去阅读其中的一些。

标签: c# sql-server asp.net-mvc visual-studio


【解决方案1】:

如 cmets 中所述,您不应将密码作为纯文本存储在数据库中。

我建议你使用加密函数并将结果存储在你的表中。

在 SQL 中:

HASHBYTES('SHA2_512', 'YourPassword')

HASBYTE 功能: https://msdn.microsoft.com/en-us/library/ms174415.aspx

你有一个很好的例子:
https://www.mssqltips.com/sqlservertip/4037/storing-passwords-in-a-secure-way-in-a-sql-server-database/

【讨论】:

    【解决方案2】:

    简短回答:这是不可能的。

    长答案: 不过,您的问题不是密码的可见性。就是密码的可恢复性。

    正如@Arnoud Gastelblum 所说,您应该散列您的密码。

    散列是一种将密码更改为不可恢复的字符串的方法。这样,即使密码字符串可见,它也没有任何意义,也无法恢复为真实密码。

    如何查看密码

    如上所述,散列是一种单向过程。一旦某事被散列,就没有回头路了。因此,当有人登录时检查密码:你对某人发送到你服务器的输入字符串进行哈希处理,然后检查这个哈希值,哈希值已经在数据库中。如果不相同,则密码错误。

    【讨论】:

      【解决方案3】:

      这是一个简单的哈希密码助手,在数据库中存储哈希值和盐值。

      public class PasswordBLL
          {
              public static bool ValidatePassword(UserObjLibrary user, string Password)
              {
                  return user.passwordHash == EncodePassword(Password, user.passwordSalt);
              }
      
              public static int ValidatePassword(string userName, string Password, string ipAddress, string MacAddress)
              {
                  UserDAL ud = new UserDAL();
                  UserObjLibrary user = ud.Details(userName:userName);
                  user.lastActivity_ip = ipAddress;
                  user.lastActive_MAC_address = MacAddress;
                  if (user != null && user.userId > 0)
                      ud.LogInActivity(user);
                  if(user == null || user.userId < 1)
                      return -1;
                  return ValidatePassword(user,Password) ? user.userId : -2;
              }
      
              public static string GenerateSalt()
              {
                  byte[] buf = new byte[16];
                  (new RNGCryptoServiceProvider()).GetBytes(buf);
                  return Convert.ToBase64String(buf);
              }
      
              public static string EncodePassword(string pass, string salt)
              {
                  try
                  {
                      byte[] bytes = Encoding.Unicode.GetBytes(pass);
                      byte[] src = Convert.FromBase64String(salt);
                      byte[] dst = new byte[src.Length + bytes.Length];
                      byte[] inArray = null;
                      Buffer.BlockCopy(src, 0, dst, 0, src.Length);
                      Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
      
                      HashAlgorithm algorithm = HashAlgorithm.Create("SHA512");
                      inArray = algorithm.ComputeHash(dst);
      
                      return Convert.ToBase64String(inArray);
                  }
                  catch (Exception ex)
                  {
                      // This gets thrown if the salt is invalid
                      return "--Invalid--";   // Any non empty value is fine to make sure the match fails
                  }
              }
          }
      

      当您将用户添加到数据库时,生成单面加密

      public int Add(UserObjLibrary user)
              {
                  UserDAL ud = new UserDAL();
                  PasswordBLL pb = new PasswordBLL();
                  user.passwordSalt = PasswordBLL.GenerateSalt();
                  user.passwordHash = PasswordBLL.EncodePassword(user.password, user.passwordSalt);
                  return ud.Add(user);
              }
      

      在用户尝试登录时验证用户凭据

      public static bool Login(string userName, string password, string ipAddress, string MacAddress)
              {
                  return PasswordBLL.ValidatePassword(userName: userName, Password: password, ipAddress: ipAddress, MacAddress: MacAddress) > 0;
              }
      

      【讨论】:

      • -1;很明显,您已经从前雇主的代码库中删减了它,而且它并不是完全由您编写的。不相关的副作用,例如在用户模型上显式存储用户的 IP 和 MAC 地址,表明这是从真实代码库中提取的,甚至没有仔细阅读它以删除不应该存在的位,并且始终正确的英语给出了- 鉴于您最近 10 个答案中超过三分之二的非抄袭句子包含拼写、语法或标点符号错误 - 这不是您自己写的。
      • @MarkAmery 现在这有点过分了。这是来自 CodePlex 的代码,也是我在 Code Plex 上编写的。有几个地方我接受了你的反对票,但我不同意这一点。
      • 代码中散布着关于 IP 和 MAC 地址的随机无关内容,即使它没有被抄袭,也有足够的理由投反对票。至于抄袭,我不确定您是声称这是来自 CodePlex 上托管的项目还是来自 CodePlex 的内部,但鉴于您没有设法在此答案或您对我的回复中写出一个正确的句子但显然设法在代码中的两个 cmets 中没有出错......好吧,如果我只是不相信你创作了它,你会原谅我的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 2016-08-31
      • 1970-01-01
      相关资源
      最近更新 更多