19

I don't know how to make custom setter for C# data model. The scenario is pretty simple, I want my password to be automatically encrypted with SHA256 function. SHA256 function works very well (I've used in in gazillion of projects before).

I've tried couple of things but when I run update-database it seems it's doing something recursively and my Visual Studio hangs (don't send error). Please help me understand how to make passwords be encrypted by default in model.

Code with what I've already tried

public class Administrator
{
    public int ID { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password
    {
        get
        {
            return this.Password;
        }

        set
        {
            // All this code is crashing Visual Studio

            // value = Infrastructure.Encryption.SHA256(value);
            // Password = Infrastructure.Encryption.SHA256(value);
            // this.Password = Infrastructure.Encryption.SHA256(value);
        }
    }
}

Seed

context.Administrators.AddOrUpdate(x => x.Username, new Administrator { Username = "admin", Password = "123" });
Stan
  • 22,856
  • 45
  • 148
  • 231
  • 2
    Are you trying to set the Accessor to itself? Password = XXX would recall the Set Accessor of Password, which would again recall it... If you let VS run long enough, I would guess you would get a stack overflow. Do you have an actual field to store your password? – LightStriker Oct 20 '12 at 14:08
  • @Marc-AndréJutras indeed it does something recursively I suspect, but how can I make this work then? – Stan Oct 20 '12 at 14:09
  • What is the field you use to store your password? An accessor isn't a field, it's a method in disguise. – LightStriker Oct 20 '12 at 14:09

2 Answers2

41

You need to use a private member variable as a backing-field. this allows you to store the value separately and manipulate it in the setter.

Good information here

public class Administrator
{
    public int ID { get; set; }

    [Required]
    public string Username { get; set; }

    private string _password;

    [Required]
    public string Password
    {
        get
        {
            return this._password;
        }

        set
        {  
             _password = Infrastructure.Encryption.SHA256(value);                
        }
    }
}
Chris Kooken
  • 29,979
  • 14
  • 79
  • 113
2

The get and set you're using actually create methods called get_Password() and set_Password(password).

You'd want the actual password to be stored in a private variable. So, just having a private variable that gets returned and updated by those "methods" is the way to go.

public class Administrator
{
public int ID { get; set; }
[Required]
public string Username { get; set; }
[Required]
private string password;
public string Password
{
    get
    {
        return this.password;
    }

    set
    {
        this.password = Infrastructure.Encryption.SHA256(value);
    }
}
}
Abdulsattar Mohammed
  • 8,916
  • 12
  • 49
  • 66