我正在做自定义的asp.net身份而不使用asp.net内置表。我已经成功创建了用户,实现了自定义even
现在我想用新的加密密码更新用户,因此我无法获得如何提供CreateAsync
的自定义实现。
这是我的表:
用户:UpdateAsync method
模型:
Id,Name,EmailId,Password,Statistics,Salary
我实现IUserstore的自定义类:
public class UserModel : IUser
{
public string Id { get; set; }
public string Name { get; set; }
public string EmailId { get; set; }
public string Password { get; set; }
public int Salary { get; set; }
}
控制器:
public class UserStore : IUserStore<UserModel>, IUserPasswordStore<UserModel>
{
private readonly MyEntities _dbContext;
private readonly HttpContext _httpContext;
// How to implement this method for updating only user password
public Task UpdateAsync(UserModel user)
{
throw new NotImplementedException();
}
public Task CreateAsync(UserModel user)
{
return Task.Factory.StartNew(() =>
{
HttpContext.Current = _httpContext ?? HttpContext.Current;
var user = _dbContext.User.Create();
user.Name = user.Name;
user.EmailId = user.EmailId;
user.EmailAddress = user.Email;
user.Password = user.Password;
_dbContext.Users.Add(dbUser);
_dbContext.SaveChanges();
});
}
public Task SetPasswordHashAsync(UserModel user, string passwordHash)
{
return Task.Factory.StartNew(() =>
{
HttpContext.Current = _httpContext ?? HttpContext.Current;
var userObj = GetUserObj(user);
if (userObj != null)
{
userObj.Password = passwordHash;
_dbContext.SaveChanges();
}
else
user.Password = passwordHash;
});
}
public Task<string> GetPasswordHashAsync(UserModel user)
{
//other code
}
}
答案 0 :(得分:2)
不确定这是不是你想要的......
public Task UpdateAsync(UserModel model)
{
return Task.Factory.StartNew(() =>
{
var user = _dbContext.User.Find(x => x.id == model.id);
user.Password = model.Password;
_dbContext.SaveChanges();
});
}
它将获取特定记录并更新密码,然后保存记录。
修改强>
由于密码未加密,我添加了代码以获取该字符串并保持模型不变,此扩展方法将加密密码的值,我没有测试这个,但我相信它会起作用。
user.Password = model.Password.EncryptPassword(EncryptKey);
答案 1 :(得分:0)