public async Task should_not_send_sms_again_in_7_days_after_verified() { const string phoneNumber = "13603503455"; var verifiedRecord = new VerifiedPhoneNumber { PhoneNumber = phoneNumber }; _phoneRepo.Save(verifiedRecord); var user = _theApp.MockUser(); user.VerifiedPhoneNumber = verifiedRecord; _userRepo.Update(user); var userCtrl = _theApp.CreateController <UserController>(); var result = await userCtrl.SendPhoneNumberVerificationCode(phoneNumber); Assert.False(result.HasSucceeded); var messageSent = _phoneVerifyRepo .All() .Any(r => r.UserId == user.Id && r.PhoneNumber == phoneNumber); Assert.False(messageSent); }
public async Task <User> ImportNewUser(IList <Claim> claims, Claim userIdClaim) { var originalUserName = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.PreferredUserName)?.Value ?? userIdClaim.Value; var userName = string.Concat(originalUserName, "@", _idpOptions.ProviderId); if (!_siteSettings.CanRegisterNewUsers()) { const string errorMessage = "已关闭用户注册"; _logger.LogWarning("用户注册失败:{@RegisterAttempt}", new { username = userName, Result = errorMessage }); throw new InvalidOperationException(errorMessage); } var displayNameClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.GivenName)?.Value ?? claims.FirstOrDefault(x => x.Type == JwtClaimTypes.NickName)?.Value; var emailClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Email)?.Value; var emailVerifiedClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.EmailVerified)?.Value; var emailVerified = false; if (!string.IsNullOrEmpty(emailClaim) && Boolean.TryParse(emailVerifiedClaim, out emailVerified)) { // nothing to do... } var phoneNumberClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.PhoneNumber)?.Value; VerifiedPhoneNumber verifiedPhoneNumber = null; var phoneNumberVerifiedClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.PhoneNumberVerified)?.Value; if (!string.IsNullOrEmpty(phoneNumberClaim) && Boolean.TryParse(phoneNumberVerifiedClaim, out var phoneNumberVerified) && phoneNumberVerified) { verifiedPhoneNumber = new VerifiedPhoneNumber() { PhoneNumber = phoneNumberClaim }; _phoneNumberVerificationRepo.Save(verifiedPhoneNumber); } var user = new User { UserName = userName, DisplayName = string.IsNullOrWhiteSpace(displayNameClaim) ? originalUserName : displayNameClaim, CreatedAtUtc = _clock.Now.UtcDateTime, EmailAddress = emailClaim, EmailAddressConfirmed = emailVerified, OpenId = userIdClaim.Value, OpenIdProvider = _idpOptions.ProviderId, LastSeenAt = _clock.Now.UtcDateTime, PhoneNumberId = verifiedPhoneNumber?.Id }; var result = await _userManager.CreateAsync(user); if (!result.Succeeded) { var errorMessage = string.Join(";", result.Errors.Select(err => err.Description ?? err.Code)); _logger.LogWarning("用户注册失败:{@LoginAttempt}", new { UserName = userName, Result = errorMessage }); throw new InvalidOperationException(errorMessage); } _logger.LogInformation("用户注册成功:{@RegisterAttempt}", new { UserName = userName, UserId = user.Id }); return(user); }