Datamembers did not head a password?

I try to use hash / salt my data passwords in my data, but when I add a new student and then GET that student collection, the password field was not hashed / salty, does it return like what I typed?

    public void AddStudent(Student student)
    {

        student.StudentID = (++eCount).ToString();
        byte[] passwordHash = Hash(student.Password, GenerateSalt());

        student.TimeAdded = DateTime.Now;
        students.Add(student);
    }

Can anyone fix this?

+3
source share
2 answers

you should assign a hashed password to the student, and then add the student.

public void AddStudent(Student student)
{
    student.StudentID = (++eCount).ToString();
    byte[] passwordHash = Hash(student.Password, GenerateSalt());

    StringBuilder stringBuilder = new StringBuilder();

    foreach(byte b in passwordHash){
        stringBuilder.AppendFormat("{0:X2}", b);
    }

    student.TimeAdded = DateTime.Now;
    student.Password= stringBuilder.ToString();;
    students.Add(student);
}
+3
source

You could just add this to your original question , but here is another code:

[DataContract(Name="Student")]
public class Student
{
    [DataMember(Name = "StudentID")]
    public string StudentID { get; set; }
    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }
    [DataMember(Name = "LastName")]
    public string LastName { get; set; }
    // local non public cache
    private byte[] _password;
    [DataMember(Name = "Password")]
    public byte[] Password {
        get { return _password; }
        set {
            this.Salt = GenerateSalt();
            this._password = Hash(value, this.Salt);
        }
    };
    [DataMember(Name = "Salt")]
    public byte[] Salt;

    // ...
+1
source

All Articles