示例#1
0
        /// <summary>
        /// SetLoginNameAsync
        /// </summary>
        /// <param name="userGuid"></param>
        /// <param name="loginName"></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 SetLoginNameAsync <TUser>(string userGuid, string loginName, TransactionContext transContext) where TUser : IdenityUser, new()
        {
            ThrowIf.NullOrNotLoginName(loginName, nameof(loginName));

            TUser?user = await GetAsync <TUser>(userGuid, transContext).ConfigureAwait(false);

            if (user == null)
            {
                throw new IdentityException(ErrorCode.IdentityNotFound, $"userGuid:{userGuid}");
            }

            if (!loginName.Equals(user.LoginName, GlobalSettings.Comparison) && 0 != await _db.CountAsync <TUser>(u => u.LoginName == loginName, transContext).ConfigureAwait(false))
            {
                throw new IdentityException(ErrorCode.IdentityAlreadyExists, $"userGuid:{userGuid}, loginName:{loginName}");
            }

            user.LoginName = loginName;

            await ChangeSecurityStampAsync(user).ConfigureAwait(false);

            await _db.UpdateAsync(user, transContext).ConfigureAwait(false);
        }