12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using Microsoft.AspNetCore.Identity;
- using VCommon.VApplication.Authorization;
- using VCommon.VOpenApi.VAspNetCore;
- using VCommonCoreExample.AppServices.Basic;
- using VCommonCoreExample.AppServices.UserProfiles.Dto;
- using VCommonCoreExample.Caching;
- using VCommonCoreExample.Entity;
- namespace VCommonCoreExample.AppServices.UserProfiles
- {
- [VServiceAuthorize]
- public interface IUserProfileService
- {
- void ChangeMyPassword(ChangeMyPasswordInput input);
- }
- public class UserProfileService : DbTableAppServiceBase<User>, IUserProfileService
- {
- private readonly AuthCache _authCache;
- private readonly SessionCache _sessionCache;
- public UserProfileService(AuthCache authCache, SessionCache sessionCache)
- {
- _authCache = authCache;
- _sessionCache = sessionCache;
- }
- public void ChangeMyPassword(ChangeMyPasswordInput input)
- {
- using var repo = GetRepository();
- var usr = repo.GetEntityOrDefault(VSession.GetUserId());
-
- var ph = new PasswordHasher<User>();
- if (ph.VerifyHashedPassword(usr, usr.Password, input.OldPassword) == PasswordVerificationResult.Failed) throw new VFriendlyException("原始密码不正确");
-
- usr.Password = ph.HashPassword(usr, input.NewPassword);
- repo.SaveChanges();
- var scm = _sessionCache.Get(VSession.Token);
- scm.HashedPassword = usr.Password;
- _sessionCache.Set(VSession.Token, scm);
- _authCache.ClearUser(usr.TenantId, usr.Id);
- }
- }
- }
|