我有一个用户表,我想用于动态数据。问题是我有使用MD5加密所需的密码字段。我正在使用实体框架,我是怎么做到的?
答案 0 :(得分:2)
另一种想法是创建一个自定义FieldTemplate(使用UIHint来覆盖字段字段模板)来加密这个字段。
答案 1 :(得分:0)
我找到了这个解决方案,但如果有人有更好的想法,请告诉我
public partial class SigecRendicionesEntities
{
partial void OnContextCreated()
{
// Register the handler for the SavingChanges event.
this.SavingChanges
+= new EventHandler(context_SavingChanges);
}
// SavingChanges event handler.
private static void context_SavingChanges(object sender, EventArgs e)
{
// Validate the state of each entity in the context
// before SaveChanges can succeed.
foreach (ObjectStateEntry entry in
((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(
EntityState.Added | EntityState.Modified))
{
// Find an object state entry for a SalesOrderHeader object.
if (entry.Entity.GetType() == typeof(Usuario))
{
Usuario usr = entry.Entity as Usuario;
string hashProvider = "MD5CryptoServiceProvider";
usr.Clave = Cryptographer.CreateHash(hashProvider, usr.Clave);
}
}
}
}