/// <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> /// CreateUserAsync /// </summary> /// <param name="mobile"></param> /// <param name="email"></param> /// <param name="loginName"></param> /// <param name="password"></param> /// <param name="mobileConfirmed"></param> /// <param name="emailConfirmed"></param> /// <param name="lastUser"></param> /// <param name="transactionContext"></param> /// <returns></returns> /// <exception cref="IdentityException"></exception> /// <exception cref="DatabaseException"></exception> public async Task <User> CreateUserAsync(string mobile, string?email, string?loginName, string?password, bool mobileConfirmed, bool emailConfirmed, string lastUser, TransactionContext?transactionContext = null) { ThrowIf.NotMobile(mobile, nameof(mobile), true); ThrowIf.NotEmail(email, nameof(email), true); ThrowIf.NotLoginName(loginName, nameof(loginName), true); ThrowIf.NotPassword(password, nameof(password), true); if (mobile == null && email == null && loginName == null) { throw Exceptions.IdentityMobileEmailLoginNameAllNull(); } if (!mobileConfirmed && !emailConfirmed && password == null) { throw Exceptions.IdentityNothingConfirmed(); } bool ownTrans = transactionContext == null; TransactionContext transContext = transactionContext ?? await _transaction.BeginTransactionAsync <User>().ConfigureAwait(false); try { long count = await _userRepo.CountUserAsync(loginName, mobile, email, transContext).ConfigureAwait(false); if (count != 0) { throw Exceptions.IdentityAlreadyTaken(mobile: mobile, email: email, loginName: loginName); } User user = new User(loginName, mobile, email, password, mobileConfirmed, emailConfirmed); await _userRepo.AddAsync(user, lastUser, transContext).ConfigureAwait(false); if (ownTrans) { await transContext.CommitAsync().ConfigureAwait(false); } return(user); } catch { if (ownTrans) { await transContext.RollbackAsync().ConfigureAwait(false); } throw; } }
/// <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); }