代码之家  ›  专栏  ›  技术社区  ›  TheCloudlessSky

什么类应该能够设置密码(存储库、用户等)?

  •  3
  • TheCloudlessSky  · 技术社区  · 15 年前

    我使用的是实体框架4。我不确定设置用户密码的责任应该在哪里。我在仓库里想 SetPassword(User user, string password)

    主要的问题是它不能只是一个简单的属性设置器,因为我还返回生成的salt。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Andy_Vulhop    15 年前

    您可能需要创建某种模块来处理要调用的用户类的密码创建。这更符合单一责任原则(一个类应该只有一个责任,有时读作“只有一个改变的理由”)。

    应该 在它们被持久化之前对它们进行盐析和散列,这绝对超出了用户应该做的范围。

        2
  •  2
  •   Niels van der Rest    15 年前

    你可以介绍一个 Password 班级。这个 密码 上的属性 User string .

    public class User
    {
      public Password Password { get; set; }
    }
    
    public class Password
    {
      private string value;
      private string salt;
    
      public Password(string value)
      {
        this.salt = "generated salt";
        this.value = value;
      }
    
      public string ToHashedString()
      {
        // Return the hashed password.
      }
    }
    

    密码 PasswordService 它使用其他依赖项来生成salt并构造 密码

        3
  •  1
  •   Amitabh    15 年前

    它不应该在存储库中,因为它应该只用于数据库访问,而不用于业务逻辑。我认为它应该在用户类中。

    public class User
    {
      IUserPasswordManager _userPasswordManager;
    
      public string SetPassword(string oldPassword, string newPassword)
      {
         //Set password and return salt
         return _userPasswordManager.SetPasswordForUser(this, oldPassword, newPAssword);  
    
      }
    
    }