UserProfileService.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Microsoft.AspNetCore.Identity;
  2. using VCommon.VApplication.Authorization;
  3. using VCommon.VOpenApi.VAspNetCore;
  4. using VCommonCoreExample.AppServices.Basic;
  5. using VCommonCoreExample.AppServices.UserProfiles.Dto;
  6. using VCommonCoreExample.Caching;
  7. using VCommonCoreExample.Entity;
  8. namespace VCommonCoreExample.AppServices.UserProfiles
  9. {
  10. [VServiceAuthorize]
  11. public interface IUserProfileService
  12. {
  13. void ChangeMyPassword(ChangeMyPasswordInput input);
  14. }
  15. public class UserProfileService : DbTableAppServiceBase<User>, IUserProfileService
  16. {
  17. private readonly AuthCache _authCache;
  18. private readonly SessionCache _sessionCache;
  19. public UserProfileService(AuthCache authCache, SessionCache sessionCache)
  20. {
  21. _authCache = authCache;
  22. _sessionCache = sessionCache;
  23. }
  24. public void ChangeMyPassword(ChangeMyPasswordInput input)
  25. {
  26. using var repo = GetRepository();
  27. var usr = repo.GetEntityOrDefault(VSession.GetUserId());
  28. var ph = new PasswordHasher<User>();
  29. if (ph.VerifyHashedPassword(usr, usr.Password, input.OldPassword) == PasswordVerificationResult.Failed) throw new VFriendlyException("原始密码不正确");
  30. usr.Password = ph.HashPassword(usr, input.NewPassword);
  31. repo.SaveChanges();
  32. var scm = _sessionCache.Get(VSession.Token);
  33. scm.HashedPassword = usr.Password;
  34. _sessionCache.Set(VSession.Token, scm);
  35. _authCache.ClearUser(usr.TenantId, usr.Id);
  36. }
  37. }
  38. }