Esempio n. 1
0
        public async Task <IActionResult> SendTwoFactorCode([FromBody] TwoFactorRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // First we need to check if this request is valid - We cannot depend on client side validation alone
            // Check the validity of TwoFactorToken & Session Expiry
            try
            {
                var result = await _userSvc.SendTwoFactorAsync(model);

                if (result.IsValid)
                {
                    // Send code to the user via to their preferred provider.
                    if (model.ProviderType.Equals("Email"))
                    {
                        var message = $"<h2>Your Two-Factor Authentication Code : {result.Code}</h2>";
                        await _emailSvc.SendEmailAsync(
                            result.Email,
                            "Two-Factor Code",
                            message,
                            "TwoFactorAuthentication.html");


                        return(Ok(new { Message = "TwoFactorCode-Send" }));
                    }
                    if (model.ProviderType.Equals("SMS"))
                    {
                        //TODO : Phase 2
                        return(BadRequest("SMS Service not implemented"));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            return(Unauthorized(new { LoginError = "Two-Factor Fail" }));
        }