Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

c# - How to give custom implementation of UpdateAsync method of asp.net identity?

I am doing custom asp.net identity and not using asp.net inbuilt tables.I have successfully created user with implementing custom CreateAsync

Now i want to update user with new encrypted password and so i am not getting how to provide custom implementation of UpdateAsync method.

This is my table:

User : Id,Name,EmailId,Password,Statistics,Salary

Model:

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

My custom class which implements 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
    }
}

Controller:

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);
    }        
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...