/// <summary> /// CreateByMobileAsync /// </summary> /// <param name="mobile"></param> /// <param name="loginName"></param> /// <param name="password"></param> /// <param name="mobileConfirmed"></param> /// <param name="transContext"></param> /// <returns></returns> /// <exception cref="HB.Component.Identity.IdentityException"></exception> /// <exception cref="ValidateErrorException"></exception> /// <exception cref="DatabaseException"></exception> public async Task <TUser> CreateByMobileAsync <TUser>(string mobile, string?loginName, string?password, bool mobileConfirmed, TransactionContext transContext) where TUser : IdenityUser, new() { ThrowIf.NullOrNotMobile(mobile, nameof(mobile)); ThrowIf.NotPassword(password, nameof(password), true); #region Existense Check TUser?user = await GetByMobileAsync <TUser>(mobile, transContext).ConfigureAwait(false); if (user != null) { throw new IdentityException(ErrorCode.IdentityMobileAlreadyTaken, $"userType:{typeof(TUser)}, mobile:{mobile}"); } if (!string.IsNullOrEmpty(loginName)) { TUser?tmpUser = await GetByLoginNameAsync <TUser>(loginName, transContext).ConfigureAwait(false); if (tmpUser != null) { throw new IdentityException(ErrorCode.IdentityLoginNameAlreadyTaken, $"userType:{typeof(TUser)}, mobile:{mobile}, loginName:{loginName}"); } } #endregion user = InitNew <TUser>(mobile, loginName, password); user.MobileConfirmed = mobileConfirmed; await _db.AddAsync(user, transContext).ConfigureAwait(false); return(user); }
/// <summary> /// SetPasswordByMobileAsync /// </summary> /// <param name="mobile"></param> /// <param name="newPassword"></param> /// <param name="transContext"></param> /// <returns></returns> /// <exception cref="HB.Component.Identity.IdentityException"></exception> /// <exception cref="ValidateErrorException"></exception> /// <exception cref="DatabaseException"></exception> public async Task SetPasswordByMobileAsync <TUser>(string mobile, string newPassword, TransactionContext transContext) where TUser : IdenityUser, new() { ThrowIf.NullOrNotMobile(mobile, nameof(mobile)); ThrowIf.NotPassword(mobile, nameof(newPassword), false); TUser?user = await GetByMobileAsync <TUser>(mobile, transContext).ConfigureAwait(false); if (user == null) { throw new IdentityException(ErrorCode.IdentityNotFound, $"mobile:{mobile}"); } user.PasswordHash = SecurityUtil.EncryptPwdWithSalt(newPassword, user.Guid); await ChangeSecurityStampAsync(user).ConfigureAwait(false); await _db.UpdateAsync(user, transContext).ConfigureAwait(false); }