开发者

How can I Hash or Encrypt a field with EF CodeFirst?

开发者 https://www.devze.com 2023-02-02 04:49 出处:网络
Using EF Code First, how can I interrupt the saving of a field value so I can hash it?A simple example would be a password field:

Using EF Code First, how can I interrupt the saving of a field value so I can hash it? A simple example would be a password field:

public class Account
{
    private string _password; 

    public string Password
    {
        get
        {
            return _password;
        }
        set
        {
           _password = MyHashMethod(value);
        }
    }
}

This appears to work when saving the value to the database, but doesn't work when retrieving the value.

EDIT: Changed _password = MyHashMethod(_password) to MyHashMethod(value) above. Sa开发者_如何转开发me correction needs to be made in the answer below.


I would just make it like:

public class Account {
    public string HashedPassword { get; set; } 
    public string ClearTextPassword { 
        set { HashedPassword = MyHashMethod(value); }
    }
}

Only HashedPassword is stored in DB.

0

精彩评论

暂无评论...
验证码 换一张
取 消