예제 #1
0
        public async Task <ActionResult> SendCode(SendCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            if (!await SignInHelper.SendTwoFactorCode(model.SelectedProvider))
            {
                return(View("Error"));
            }
            return(RedirectToAction("VerifyCode", new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl }));
        }
예제 #2
0
        protected void ProviderSubmit_Click(object sender, EventArgs e)
        {
            if (!signinHelper.SendTwoFactorCode(Providers.SelectedValue))
            {
                Response.Redirect("/Account/Error");
            }

            var user = manager.FindById(signinHelper.GetVerifiedUserId());

            if (user != null)
            {
                // To exercise the flow without actually sending codes, uncomment the following line
                DemoText.Text = "For DEMO purposes the current " + Providers.SelectedValue + " code is: " + manager.GenerateTwoFactorToken(user.Id, Providers.SelectedValue);
            }

            SelectedProvider.Value = Providers.SelectedValue;
            sendcode.Visible       = false;
            verifycode.Visible     = true;
        }
        public async Task <ActionResult> TokenLogin(LoginViewModel model, string returnUrl)
        {
            ViewBag.ReturnUrl           = returnUrl;
            ViewBag.LiveDemoMemberships = Demo.GetLiveDemoMemberships();
            ViewBag.EnableDemo          = (ViewBag.LiveDemoMemberships != null && ViewBag.LiveDemoMemberships.Count > 0);
            ViewBag.EnableMobil         = UserManager.TwoFactorProviders.ContainsKey("PhoneCode");
            model.LoginState            = LoginViewModel.State.TokenLogin;

            if (!model.MobilNumberValidated)
            {
                if (!MobilNumberValidator.IsValid(model.MobilNumber, true))
                {
                    ModelState.AddModelError("MobilNumber", "Der blev ikke fundet en pilot med dette nummer.");
                    return(View("Login", model));
                }
                else
                {
                    model.MobilNumberValidated = true;
                    model.MobilNumber          = MobilNumberValidator.ParseMobilNumber(model.MobilNumber);

                    var result = await SignInHelper.MobilSignIn(model.MobilNumber, model.RememberBrowser);

                    switch (result)
                    {
                    case SignInStatus.LockedOut:
                        return(View("Lockout"));

                    case SignInStatus.UnConfirmed:
                    case SignInStatus.Success:
                    case SignInStatus.RequiresTwoFactorAuthentication:
                        model.MobilNumberValidated = true;

                        // Does not function because we are working prior to cookies being written.
                        //var userId = await SignInHelper.GetVerifiedUserIdAsync();
                        var user = await UserManager.FindByNameAsync(model.MobilNumber);

                        var userId = user.Id;
                        if (userId == null)
                        {
                            model.MobilNumberValidated = false;
                            ModelState.AddModelError("MobilNumber", "Unable to find verified user");
                            return(View("Login", model));
                        }
                        var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(userId);

                        if (userFactors.All(p => p != "PhoneCode"))
                        {
                            model.MobilNumberValidated = false;
                            ModelState.AddModelError("MobilNumber", "SMS Provider not available");
                            return(View("Login", model));
                        }

                        if (!await SignInHelper.SendTwoFactorCode("PhoneCode", userId))
                        {
                            model.MobilNumberValidated = false;
                            ModelState.AddModelError("MobilNumber", "Unable to send verification code");
                            return(View("Login", model));
                        }

                        if (HttpContext.IsDebuggingEnabled)
                        {
                            // To exercise the flow without actually sending codes, uncomment the following line
                            ModelState.AddModelError("VerifyCode", "For DEMO purposes the current verification code is: " + await UserManager.GenerateTwoFactorTokenAsync(userId, "PhoneCode"));
                        }

                        return(View("Login", model));

                    case SignInStatus.Failure:
                    default:
                        model.MobilNumberValidated = false;
                        ModelState.AddModelError("MobilNumber", "Unable to sign-in");
                        return(View("Login", model));
                    }
                }
            }

            // Ready to handle Verification Code
            if (!string.IsNullOrWhiteSpace(model.VerifyCode))
            {
                var result = await SignInHelper.TwoFactorSignIn("PhoneCode", model.VerifyCode, model.RememberBrowser, false);

                switch (result)
                {
                case SignInStatus.Success:
                    return(RedirectToLocal(returnUrl));

                case SignInStatus.UnConfirmed:
                    return(RedirectToAction("EmailNotConfirmed"));    // State should not be possible to reach on mobil login accounts

                case SignInStatus.LockedOut:
                    return(View("Lockout"));

                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("VerifyCode", "Invalid code");
                    return(View("Login", model));
                }
            }

            return(View("Login", model));
        }