开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜