private void ValidateAccount(AccountRegisterRequestInfo account) { string accountNamePattern = @"^[a-zA-Z0-9.@]*$"; var accountNameRegex = new Regex(accountNamePattern, RegexOptions.IgnoreCase); if (account == null) { throw new UCenterException(UCenterErrorCode.InvalidAccountName); } if (string.IsNullOrEmpty(account.AccountName) || !accountNameRegex.IsMatch(account.AccountName) || account.AccountName.Length < 4 || account.AccountName.Length > 64) { throw new UCenterException(UCenterErrorCode.InvalidAccountName); } if (string.IsNullOrEmpty(account.Password) || account.Password.Length < 6 || account.Password.Length > 64) { throw new UCenterException(UCenterErrorCode.InvalidAccountPassword); } if (string.IsNullOrEmpty(account.SuperPassword) || account.SuperPassword.Length < 6 || account.Password.Length > 64) { throw new UCenterException(UCenterErrorCode.InvalidAccountPassword); } //if (string.IsNullOrEmpty(account.Phone)) //{ // throw new UCenterException(UCenterErrorCode.InvalidAccountPhone); //} if (!string.IsNullOrEmpty(account.Email)) { string emailPattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"; var emailRegex = new Regex(emailPattern, RegexOptions.IgnoreCase); if (!emailRegex.IsMatch(account.Email)) { throw new UCenterException(UCenterErrorCode.InvalidAccountEmail); } } }
public async Task <IHttpActionResult> Register([FromBody] AccountRegisterRequestInfo info, CancellationToken token) { this.Logger.Info($"Account.Register AccountName={info.AccountName}"); var removeTempsIfError = new List <AccountResourceEntity>(); var error = false; try { var account = await DatabaseContext.Bucket.FirstOrDefaultAsync <AccountEntity>( a => a.AccountName == info.AccountName, false); if (account != null) { throw new UCenterException(UCenterErrorCode.AccountRegisterFailedAlreadyExist); } account = new AccountEntity { AccountName = info.AccountName, IsGuest = false, Name = info.Name, IdentityNum = info.IdentityNum, Password = EncryptHashManager.ComputeHash(info.Password), SuperPassword = EncryptHashManager.ComputeHash(info.SuperPassword), PhoneNum = info.PhoneNum, Sex = info.Sex, CreatedDateTime = DateTime.UtcNow }; if (!string.IsNullOrEmpty(account.AccountName)) { var namePointer = new AccountResourceEntity(account, AccountResourceType.AccountName); await this.DatabaseContext.Bucket.InsertSlimAsync(namePointer); removeTempsIfError.Add(namePointer); } if (!string.IsNullOrEmpty(account.PhoneNum)) { var phonePointer = new AccountResourceEntity(account, AccountResourceType.Phone); await this.DatabaseContext.Bucket.InsertSlimAsync(phonePointer); removeTempsIfError.Add(phonePointer); } else if (!string.IsNullOrEmpty(account.Email)) { var emailPointer = new AccountResourceEntity(account, AccountResourceType.Email); await this.DatabaseContext.Bucket.InsertSlimAsync(emailPointer); removeTempsIfError.Add(emailPointer); } // set the default profiles account.ProfileImage = await this.storageContext.CopyBlobAsync( account.Sex == Sex.Female ?this.settings.DefaultProfileImageForFemaleBlobName : this.settings.DefaultProfileImageForMaleBlobName, this.settings.ProfileImageForBlobNameTemplate.FormatInvariant(account.Id), token); account.ProfileThumbnail = await this.storageContext.CopyBlobAsync( account.Sex == Sex.Female ?this.settings.DefaultProfileThumbnailForFemaleBlobName : this.settings.DefaultProfileThumbnailForMaleBlobName, this.settings.ProfileThumbnailForBlobNameTemplate.FormatInvariant(account.Id), token); await this.DatabaseContext.Bucket.InsertSlimAsync(account); return(CreateSuccessResult(ToResponse <AccountRegisterResponse>(account))); } catch (Exception ex) { this.Logger.Info($"Account.Register Exception:AccoundName={info.AccountName}"); this.Logger.Info(ex.ToString()); error = true; if (ex is CouchBaseException) { var status = (ex as CouchBaseException).Result as IDocumentResult <AccountResourceEntity>; if (status != null) { throw new UCenterException(UCenterErrorCode.AccountRegisterFailedAlreadyExist, ex); } } throw; } finally { if (error) { foreach (var item in removeTempsIfError) { this.DatabaseContext.Bucket.Remove(item.ToDocument()); } } } }
public async Task <IActionResult> Register([FromBody] AccountRegisterRequestInfo info, CancellationToken token) { // 检测注册信息合法性 ValidateAccount(info); try { // 检测帐号名是否可以被注册 var oldAccountEntity = await this.Database.Accounts.GetSingleAsync( a => a.AccountName == info.AccountName, //|| a.Email == info.Email //|| a.Phone == info.Phone, token); if (oldAccountEntity != null) { throw new UCenterException(UCenterErrorCode.AccountNameAlreadyExist); } // 检查Device是否绑定了游客号 AccountEntity accountEntity = null; if (info != null && !string.IsNullOrEmpty(info.AppId) && info.Device != null && !string.IsNullOrEmpty(info.Device.Id)) { string guestDeviceId = $"{info.AppId}_{info.Device.Id}"; var guestDeviceEntity = await this.Database.GuestDevices.GetSingleAsync(guestDeviceId, token); if (guestDeviceEntity != null) { accountEntity = await this.Database.Accounts.GetSingleAsync(guestDeviceEntity.AccountId, token); } } bool guestConvert = true; if (accountEntity == null) { guestConvert = false; // 没有绑定游客号,正常注册 accountEntity = new AccountEntity { Id = Guid.NewGuid().ToString(), }; } accountEntity.AccountName = info.AccountName; accountEntity.AccountType = AccountType.NormalAccount; accountEntity.AccountStatus = AccountStatus.Active; accountEntity.Name = info.Name; accountEntity.Identity = info.Identity; accountEntity.Password = EncryptHelper.ComputeHash(info.Password); accountEntity.SuperPassword = EncryptHelper.ComputeHash(info.SuperPassword); accountEntity.Phone = info.Phone; accountEntity.Email = info.Email; accountEntity.Gender = info.Gender; //var placeholders = new[] // { // this.GenerateKeyPlaceholder(accountEntity.AccountName, KeyType.Name, accountEntity.Id, accountEntity.AccountName), // this.GenerateKeyPlaceholder(accountEntity.Phone, KeyType.Phone, accountEntity.Id, accountEntity.AccountName), // this.GenerateKeyPlaceholder(accountEntity.Email, KeyType.Email, accountEntity.Id, accountEntity.AccountName) // }; //foreach (var placeholder in placeholders) //{ // if (!string.IsNullOrEmpty(placeholder.Name)) // { // await this.Database.KeyPlaceholders.InsertAsync(placeholder, token); // removeTempsIfError.Add(placeholder); // } //} if (guestConvert) { // 绑定了游客号,游客号转正 await this.Database.Accounts.UpsertAsync(accountEntity, token); try { await this.Database.GuestDevices.DeleteAsync( d => d.AppId == info.AppId && d.AccountId == accountEntity.Id, token); } catch (Exception ex) { CustomTrace.TraceError(ex, "Error to remove guest device"); } await this.TraceAccountEvent(accountEntity, "GuestConvert", token : token); } else { // 没有绑定游客号,正常注册 // Remove this from UCenter for performance concern // If user does not have default profile icon, app client should use local default one // accountEntity.ProfileImage = await this.storageContext.CopyBlobAsync( // accountEntity.Gender == Gender.Female ? this.settings.DefaultProfileImageForFemaleBlobName : this.settings.DefaultProfileImageForMaleBlobName, // this.settings.ProfileImageForBlobNameTemplate.FormatInvariant(accountEntity.Id), // token); // accountEntity.ProfileThumbnail = await this.storageContext.CopyBlobAsync( // accountEntity.Gender == Gender.Female // ? this.settings.DefaultProfileThumbnailForFemaleBlobName // : this.settings.DefaultProfileThumbnailForMaleBlobName, // this.settings.ProfileThumbnailForBlobNameTemplate.FormatInvariant(accountEntity.Id), // token); await this.Database.Accounts.InsertAsync(accountEntity, token); await TraceAccountEvent(accountEntity, "Register", info.Device, token : token); } if (info.Device != null) { await LogDeviceInfo(info.Device, token); } return(this.CreateSuccessResult(this.ToResponse <AccountRegisterResponse>(accountEntity))); } catch (Exception ex) { CustomTrace.TraceError(ex, "Account.Register Exception:AccoundName={info.AccountName}"); throw; } }
public async Task <IHttpActionResult> Register([FromBody] AccountRegisterRequestInfo info, CancellationToken token) { CustomTrace.TraceInformation($"Account.Register AccountName={info.AccountName}"); if (!ValidateAccountName(info.AccountName)) { // TODO: Change to AccountRegisterFailedInvalidName in next client refresh throw new UCenterException(UCenterErrorCode.AccountRegisterFailedAlreadyExist); } var removeTempsIfError = new List <KeyPlaceholderEntity>(); var error = false; try { var account = await this.Database.Accounts.GetSingleAsync(a => a.AccountName == info.AccountName, token); if (account != null) { throw new UCenterException(UCenterErrorCode.AccountRegisterFailedAlreadyExist); } account = new AccountEntity { Id = Guid.NewGuid().ToString(), AccountName = info.AccountName, IsGuest = false, Name = info.Name, Email = info.Email, IdentityNum = info.IdentityNum, Password = EncryptHashManager.ComputeHash(info.Password), SuperPassword = EncryptHashManager.ComputeHash(info.SuperPassword), PhoneNum = info.PhoneNum, Sex = info.Sex }; var placeholders = new[] { this.GenerateKeyPlaceholder(account.AccountName, KeyType.Name, account.Id, account.AccountName), this.GenerateKeyPlaceholder(account.PhoneNum, KeyType.Phone, account.Id, account.AccountName), this.GenerateKeyPlaceholder(account.Email, KeyType.Email, account.Id, account.AccountName) }; foreach (var placeholder in placeholders) { if (!string.IsNullOrEmpty(placeholder.Name)) { await this.Database.KeyPlaceholders.InsertAsync(placeholder, token); removeTempsIfError.Add(placeholder); } } // set the default profiles account.ProfileImage = await this.storageContext.CopyBlobAsync( account.Sex == Sex.Female?this.settings.DefaultProfileImageForFemaleBlobName : this.settings.DefaultProfileImageForMaleBlobName, this.settings.ProfileImageForBlobNameTemplate.FormatInvariant(account.Id), token); account.ProfileThumbnail = await this.storageContext.CopyBlobAsync( account.Sex == Sex.Female ?this.settings.DefaultProfileThumbnailForFemaleBlobName : this.settings.DefaultProfileThumbnailForMaleBlobName, this.settings.ProfileThumbnailForBlobNameTemplate.FormatInvariant(account.Id), token); await this.Database.Accounts.InsertAsync(account, token); return(this.CreateSuccessResult(this.ToResponse <AccountRegisterResponse>(account))); } catch (Exception ex) { CustomTrace.TraceError(ex, "Account.Register Exception:AccoundName={info.AccountName}"); error = true; if (ex is MongoWriteException) { var mex = ex as MongoWriteException; if (mex.WriteError.Category == ServerErrorCategory.DuplicateKey) { throw new UCenterException(UCenterErrorCode.AccountRegisterFailedAlreadyExist, ex); } } throw; } finally { if (error) { try { foreach (var item in removeTempsIfError) { this.Database.KeyPlaceholders.DeleteAsync(item, token).Wait(token); } } catch (Exception ex) { CustomTrace.TraceError(ex, "Error to remove placeholder"); } } } }