2

我正在做自定义 asp.net 身份而不使用 asp.net 内置表。我已经成功创建了用户并实现了自定义CreateAsync

现在我想用新的加密密码更新用户,所以我不知道如何提供UpdateAsync method.

这是我的桌子:

用户Id,Name,EmailId,Password,Statistics,Salary

型号

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; }
}

我实现 IUserstore 的自定义类:

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
    }
}

控制器:

public class MyController : ParentController
{
    public MyController()
        : this(new UserManager<UserModel>(new UserStore(new MyEntities())))
    {
    }

    public UserManager<UserModel> UserManager { get; private set; }

    [HttpPost]
    public async Task<JsonResult> SaveUser(UserModel userModel)
    {
        IdentityResult result = null;
        if (userModel.Id > 0) //want to update user with new encrypted password
            result = await UserManager.UpdateAsync(user);
        else
            result = await UserManager.CreateAsync(userModel.EmailId, userModel.Password);
    }        
}
4

2 回答 2

2

Not sure if this is what your looking for...

public Task UpdateAsync(UserModel model)
{
    var user = _dbContext.User.Find(x => x.id == model.id);
    user.Password = model.Password;
    _dbContext.SaveChanges();
    return Task.CompletedTask;
}

It Will get the specific record and Update the password and then save the record.

Edit

Since the password is not getting encrypted i added code to take that string and leave the model as it is, this extension method will encrypt the value of password, i have not test this but i am sure it will work.

 user.Password = model.Password.EncryptPassword(EncryptKey);

Extension methods to encrypt password

于 2016-09-01T17:09:52.430 回答
0

前一段时间,我创建了一个完整的 .NET Identity 包装器,代码可以在这里找到。它可能对你有帮助。您还可以在此处找到 nuget 。我还在此处的博客中解释了该库。

于 2017-07-03T17:26:06.233 回答