如何使用EF CodeFirst散列或加密字段?

Mic*_*Cox 4 entity-framework code-first

使用EF代码首先,如何中断保存字段值以便我可以对其进行哈希处理?一个简单的例子是密码字段:

public class Account
{
    private string _password; 

    public string Password
    {
        get
        {
            return _password;
        }
        set
        {
           _password = MyHashMethod(value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这在将值保存到数据库时似乎有效,但在检索值时不起作用.

编辑:将_password = MyHashMethod(_password)更改为上面的MyHashMethod(值).需要在下面的答案中进行相同的修正.

Luk*_*Led 7

我会这样做:

public class Account {
    public string HashedPassword { get; set; } 
    public string ClearTextPassword { 
        set { HashedPassword = MyHashMethod(value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

只有HashedPassword存储在DB中.