Пример #1
0
        protected void CodeSubmit_Click(object sender, EventArgs e)
        {
            bool rememberMe = false;

            bool.TryParse(Request.QueryString["RememberMe"], out rememberMe);

            var result = signinManager.TwoFactorSignIn <ApplicationUser, string>(SelectedProvider.Value, Code.Text, isPersistent: rememberMe, 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;
            }
        }
        /// <summary>
        /// This method executes when the user clicks the verify button in
        /// the verify code section and it attempts to log the user in with
        /// the code they entered
        /// </summary>
        /// <param name="sender">The btnVerifyCode DevEx button</param>
        /// <param name="e">The Click event</param>
        protected void btnVerifyCode_Click(object sender, EventArgs e)
        {
            //Only continue if the validation is successful
            if (ASPxEdit.AreEditorsValid(this, btnVerifyCode.ValidationGroup))
            {
                //Try to sign the user in
                var result = signinManager.TwoFactorSignIn <PyramidUser, string>(hfSelectedProvider.Value, txtCode.Text, isPersistent: false, rememberBrowser: chkRememberBrowser.Checked);
                switch (result)
                {
                case SignInStatus.Success:
                    //Get the user ID
                    string userID = signinManager.GetVerifiedUserId <PyramidUser, string>();

                    //Get the user
                    var user = manager.FindById(userID);

                    //Get the user's program roles
                    List <UserProgramRole> userProgramRoles;
                    using (PyramidContext context = new PyramidContext())
                    {
                        userProgramRoles = context.UserProgramRole.Where(upr => upr.Username == user.UserName).ToList();
                    }

                    //Redirect the user based on the number of roles they have
                    if (userProgramRoles.Count > 1)
                    {
                        //Redirect the user to the select role page
                        Response.Redirect(String.Format("/Account/SelectRole.aspx?ReturnUrl={0}&message={1}",
                                                        (Request.QueryString["ReturnUrl"] != null ? Request.QueryString["ReturnUrl"].ToString() : "/Default.aspx"),
                                                        "TwoFactorVerified"));
                    }
                    else
                    {
                        //Get the UserProgramRole
                        UserProgramRole programRole = userProgramRoles.FirstOrDefault();

                        //Set the session variables
                        Session["CodeProgramRoleFK"] = programRole.CodeProgramRole.CodeProgramRolePK;
                        Session["ProgramRoleName"]   = programRole.CodeProgramRole.RoleName;
                        Session["ProgramFK"]         = programRole.ProgramFK;
                        Session["ProgramName"]       = programRole.Program.ProgramName;

                        //Redirect the user
                        Response.Redirect(Request.QueryString["ReturnUrl"] != null ? Request.QueryString["ReturnUrl"].ToString() : "/Default.aspx?message=TwoFactorVerified");
                    }
                    break;

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

                case SignInStatus.Failure:
                default:
                    msgSys.ShowMessageToUser("danger", "Invalid Code", "The code you entered is invalid!", 25000);
                    break;
                }
            }
        }