コード例 #1
0
        public async Task <ActionResult> ActivationRequestAsync()
        {
            var query = new AccountProfileGetFirstSchema {
                LinkedId = CurrentAccount.Username
            };

            if (CurrentAccount.Username.IsPhoneNumber())
            {
                query.TypeId = AccountProfileType.Phone.ToInt();
            }
            else if (new EmailAddressAttribute().IsValid(CurrentAccount.Username))
            {
                query.TypeId = AccountProfileType.Email.ToInt();
            }
            else
            {
                return(BadRequest(_localizer[DataTransferer.InvalidEmailOrCellPhone().Message]));
            }

            try {
                var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true);

                if (accountProfile == null)
                {
                    if (query.TypeId == AccountProfileType.Phone.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.PhoneNotFound().Message]));
                    }

                    if (query.TypeId == AccountProfileType.Email.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.EmailNotFound().Message]));
                    }
                }

                var activationCode = _randomMaker.NewNumber(10000, 99999);
                _memoryCache.Set(CurrentAccount.Username, activationCode, DateTime.Now.AddMinutes(10));

                if (query.TypeId == AccountProfileType.Phone.ToInt())
                {
                    await _smsService.SendAsync(new SMSModel {
                        PhoneNo  = accountProfile.LinkedId,
                        TextBody = $"{DataTransferer.ActivationCodeSMSBody().Message} \r\n {activationCode}"
                    }).ConfigureAwait(false);
                }
                else
                {
                    await _emailService.SendAsync(new EmailModel {
                        Address    = accountProfile.LinkedId,
                        Subject    = _localizer[DataTransferer.ActivationCodeEmailSubject().Message],
                        IsBodyHtml = true,
                        Body       = $"<p>{DataTransferer.ActivationCodeEmailBody().Message}</p>" +
                                     $"<p>{activationCode}</p>"
                    }).ConfigureAwait(false);
                }

                return(Ok(_localizer[DataTransferer.ActivationCodeRequested().Message]));
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem());
            }
        }