public async Task <ActionResult> VerifyCode(VerifyCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Users.Infrastructure.Helpers.SignInStatus result = await SignInHelper.TwoFactorSignIn(model.Provider, model.Code, isPersistent : false, rememberBrowser : model.RememberBrowser);

            switch (result)
            {
            case Users.Infrastructure.Helpers.SignInStatus.Success:
                var ident = HttpContext.User.Identity as ClaimsIdentity;
                ident.AddClaims(AdministratorClaimsProvider.AddAdministratorAccessToRoles(this, ident));
                HttpContext.GetOwinContext().Authentication.User.AddIdentity(ident);
                return(RedirectToLocal(model.ReturnUrl));

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

            case Users.Infrastructure.Helpers.SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid code.");
                return(View(model));
            }
        }
Exemplo n.º 2
0
        public async Task<ActionResult> VerifyCode(VerifyCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var result = await SignInHelper.TwoFactorSignIn(model.Provider, model.Code, isPersistent: false, rememberBrowser: model.RememberBrowser);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(model.ReturnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid code.");
                    return View(model);
            }
        }
Exemplo n.º 3
0
        protected void CodeSubmit_Click(object sender, EventArgs e)
        {
            var result = signinHelper.TwoFactorSignIn(SelectedProvider.Value, Code.Text, isPersistent: false, rememberBrowser: RememberBrowser.Checked);

            switch (result)
            {
            case SignInStatus.Success:
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                break;

            case SignInStatus.LockedOut:
                Response.Redirect("/Account/Lockout");
                break;

            case SignInStatus.Failure:
            default:
                FailureText.Text     = "Invalid code";
                ErrorMessage.Visible = true;
                break;
            }
        }
        public async Task <ActionResult> VerifyCode(VerifyCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // 以下代码可以防范双重身份验证代码遭到暴力破解攻击。
            // 如果用户输入错误代码的次数达到指定的次数,则会将
            // 该用户帐户锁定指定的时间。
            // 可以在 IdentityConfig 中配置帐户锁定设置
            //var result = await SignInManager.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent:  model.RememberMe, rememberBrowser: model.RememberBrowser);
            //switch (result)
            //{
            //    case SignInStatus.Success:
            //        return RedirectToLocal(model.ReturnUrl);
            //    case SignInStatus.LockedOut:
            //        return View("Lockout");
            //    case SignInStatus.Failure:
            //    default:
            //        ModelState.AddModelError("", "代码无效。");
            //        return View(model);
            //}
            var result = await SignInHelper.TwoFactorSignIn(model.Provider, model.Code, isPersistent : model.RememberMe, rememberBrowser : model.RememberBrowser);

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

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

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "代码无效。");
                return(View(model));
            }
        }
        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));
        }