예제 #1
0
        public async Task <IActionResult> ForgotPasswordAsync([FromBody] ForgotPasswordBindingModel collection)
        {
            Log.Debug($"ForgotPassword => {JsonConvert.SerializeObject(collection)}");

            if (string.IsNullOrEmpty(collection?.Username))
            {
                return(BadRequest(_localizer[DataTransferer.DefectiveEmailOrCellPhone().Message]));
            }
            collection.Username = collection.Username.Trim();

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

                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 token         = _randomMaker.NewToken();
                var username      = Convert.ToBase64String(Encoding.UTF8.GetBytes(collection.Username));
                var changepassurl = $"clipboardy.com/api/account/changepasswordrequested?username={username}&token={token}";
                _memoryCache.Set(username, token, DateTime.Now.AddMinutes(10));

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

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