/// <summary>
        /// 创建并发送短信验证码
        /// </summary>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="tag">业务标记</param>
        /// <param name="repeatSecs">多少秒内不得重复发送</param>
        /// <param name="expiredTime">过期时间</param>
        /// <returns></returns>
        public virtual async Task CreateAndSendVerificationMessage(string phoneNumber, string tag = null, int repeatSecs = 60,
                                                                   DateTime?expiredTime           = null)
        {
            var code = await Create(phoneNumber, tag, repeatSecs, expiredTime);

            await _smsSender.SendCodeAsync(phoneNumber, code);
        }
        public async Task SendPasswordResetCode(SendPasswordResetCodeInput input)
        {
            var user = GetUserByChecking(input.PhoneNumber);

            user.SetNewPasswordResetCode();
            //发送短信密码重置验证码
            await _smsSender.SendCodeAsync(user.PhoneNumber, user.PasswordResetCode);
        }
Exemplo n.º 3
0
        public async Task SendVerificationSms()
        {
            var user = await GetCurrentUserAsync();

            var code      = RandomHelper.GetRandom(100000, 999999).ToString();
            var cacheKey  = AbpSession.ToUserIdentifier().ToString();
            var cacheItem = new SmsVerificationCodeCacheItem {
                Code = code
            };

            _cacheManager.GetSmsVerificationCodeCache().Set(
                cacheKey,
                cacheItem
                );

            await _smsSender.SendCodeAsync(user.PhoneNumber, code);
        }
        public async Task SendTwoFactorAuthCode([FromBody] SendTwoFactorAuthCodeModel model)
        {
            var cacheKey = new UserIdentifier(AbpSession.TenantId, model.UserId).ToString();

            var cacheItem = await _cacheManager
                            .GetTwoFactorCodeCache()
                            .GetOrDefaultAsync(cacheKey);

            if (cacheItem == null)
            {
                //There should be a cache item added in Authenticate method! This check is needed to prevent sending unwanted two factor code to users.
                throw new UserFriendlyException(L("SendSecurityCodeErrorMessage"));
            }

            var user = await _userManager.FindByIdAsync(model.UserId.ToString());

            if (model.Provider != GoogleAuthenticatorProvider.Name)
            {
                cacheItem.Code = await _userManager.GenerateTwoFactorTokenAsync(user, model.Provider);

                var message = L("EmailSecurityCodeBody", cacheItem.Code);

                if (model.Provider == "Email")
                {
                    await _emailSender.SendAsync(await _userManager.GetEmailAsync(user), L("EmailSecurityCodeSubject"),
                                                 message);
                }
                else if (model.Provider == "Phone")
                {
                    await _smsSender.SendCodeAsync(await _userManager.GetPhoneNumberAsync(user), cacheItem.Code);
                }
            }

            _cacheManager.GetTwoFactorCodeCache().Set(
                cacheKey,
                cacheItem
                );
            _cacheManager.GetCache("ProviderCache").Set(
                "Provider",
                model.Provider
                );
        }