Sto facendo un’id quadro personalizzata di asp.net e non utilizzo le tabelle integrate di asp.net. Ho creato con successo l’utente con l’implementazione di CreateAsync
personalizzato
Ora voglio aggiornare l’utente con una nuova password crittografata e quindi non ho UpdateAsync method
come fornire l’implementazione personalizzata del UpdateAsync method
.
Questa è la mia tabella:
Utente : Id,Name,EmailId,Password,Statistics,Salary
Modello :
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; } }
La mia class personalizzata che implementa IUserstore:
public class UserStore : IUserStore, IUserPasswordStore { 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 GetPasswordHashAsync(UserModel user) { //other code } }
controller:
public class MyController : ParentController { public MyController() : this(new UserManager(new UserStore(new MyEntities()))) { } public UserManager UserManager { get; private set; } [HttpPost] public async Task 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); } }
Non sono sicuro se questo è ciò che stai cercando …
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(); }); }
Otterrà il record specifico e aggiorna la password e quindi salva il record.
modificare
Poiché la password non viene crittografata, ho aggiunto del codice per prendere quella stringa e lasciare il modello così com’è, questo metodo di estensione crittograferà il valore della password, non l’ho verificato ma sono sicuro che funzionerà.
user.Password = model.Password.EncryptPassword(EncryptKey);
Metodi di estensione per crittografare la password
Qualche tempo fa ho creato un wrapper completo su .NET Identity e il codice può essere trovato qui . Potrebbe essere utile per te. Puoi anche trovare nuget qui . Ho anche spiegato la biblioteca in un blog qui .