示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.AllKeys.Contains("code"))
            {
                var code     = Request.QueryString["code"];
                var provider = new TokenFlow.Provider();

                Task.Run(async() => { _tokens = await provider.ObtainTokens(code); }).Wait();
                Session["Tokens"] = _tokens;

                string               userId        = _tokens.IDToken.Subject;
                User                 user          = Factory.UserClient.Get(userId);
                UserFactorsClient    factorsClient = Factory.UserClient.GetUserFactorsClient(user);
                IEnumerable <Factor> factors       = factorsClient.Where(f => f.FactorType == FactorType.Sms);
                bool                 hasSmsFactor  = factors.Count() == 1;

                if (!hasSmsFactor && !string.IsNullOrEmpty(user.Profile.MobilePhone))
                {
                    // send the user to the SMS enrollment page if they aren't enrolled
                    // and their profile has a phone number
                    Session["DestinationUrl"] = "/EnrollFactor.aspx";
                }

                if (Session["DestinationUrl"] != null)
                {
                    var uri = Session["DestinationUrl"];
                    Session["DestinationUrl"] = null;
                    Response.Redirect(uri.ToString());
                }
                else
                {
                    Response.Redirect("~/");
                }
            }
        }
        protected void btnResetSms_Click(object sender, EventArgs e)
        {
            User user = Factory.UserClient.Where <User>(u => u.Profile.Login.Equals(this.tbUsername.Text)).FirstOrDefault();
            UserFactorsClient factorsClient = Factory.UserClient.GetUserFactorsClient(user);
            Factor            smsFactor     = factorsClient.First(x => x.FactorType == FactorType.Sms);
            ChallengeResponse response      = factorsClient.BeginChallenge(smsFactor);

            this.lblMessage.Text = "An SMS message was sent to your phone number on file.";
        }
        private void NonPostBackEvents()
        {
            this.btnDone.Visible = false;
            string userId = this.Master.Profile.Id;
            User   user   = Factory.UserClient.Get(userId);

            if (!string.IsNullOrEmpty(user.Profile.MobilePhone))
            {
                // add SMS as a factor, if there's a phone number
                UserFactorsClient factorsClient = Factory.UserClient.GetUserFactorsClient(user);
                Factor            to_enroll     = new Factor
                {
                    FactorType = FactorType.Sms,
                    Provider   = "OKTA",
                    Profile    = { PhoneNumber = user.Profile.MobilePhone }
                };
                Factor factor = factorsClient.Enroll(to_enroll);
                this.tbFactorID.Value = factor.Id;
            }
        }
        public void AssignFactorToUser()
        {
            TestUser dbUser = Helpers.GetUser(TestContext);
            string   strEx  = string.Empty;

            if (dbUser.Factors != null && dbUser.Factors.Count > 0)
            {
                Models.User existingUser = null;
                string      strUserLogin = dbUser.Login;

                try
                {
                    var usersClient = oktaClient.GetUsersClient();
                    existingUser = usersClient.Get(strUserLogin);

                    Assert.IsNotNull(existingUser, "Okta user {0} does not exist", dbUser.Login);

                    if (existingUser != null)
                    {
                        UserFactorsClient factorsClient = oktaClient.GetUserFactorsClient(existingUser);

                        foreach (string strFactor in dbUser.Factors)
                        {
                            Models.Factor orgFactor = orgFactorsClient.GetFactor(strFactor);
                            if (orgFactor != null && orgFactor.Status == "ACTIVE")
                            {
                                Models.Factor userFactor = factorsClient.Enroll(orgFactor);
                                Assert.IsTrue(userFactor.Status == "ACTIVE", string.Format("Factor {0} status for user {1} is {2}", orgFactor.Id, dbUser.Login, userFactor.Status));
                            }
                        }
                    }
                }
                catch (OktaException e)
                {
                    strEx = string.Format("Error Code: {0} - Summary: {1} - Message: {2}", e.ErrorCode, e.ErrorSummary, e.Message);
                }
            }
        }
        protected void btnVerifySms_Click(object sender, EventArgs e)
        {
            string            passcode      = this.tbSmsCode.Text;
            User              user          = Factory.UserClient.Where <User>(u => u.Profile.Login.Equals(this.tbUsername.Text)).FirstOrDefault();
            UserFactorsClient factorsClient = Factory.UserClient.GetUserFactorsClient(user);
            Factor            smsFactor     = factorsClient.First(x => x.FactorType == FactorType.Sms);
            MfaAnswer         answer        = new MfaAnswer {
                Passcode = passcode
            };
            ChallengeResponse response = factorsClient.CompleteChallenge(smsFactor, answer);

            if (response.FactorResult == "SUCCESS")
            {
                Uri    uri           = Factory.UserClient.ForgotPassword(user, false);
                string recoveryToken = this.GetRecoveryTokenFromUri(uri);
                string resetLink     = string.Format("{0}/ResetPassword?ott={1}", "http://localhost:8080", recoveryToken);
                Response.Redirect(resetLink);
            }
            else
            {
                // TODO what to do if the verification fails?
                lblMessage.Text = string.Format("Result {0}: {1}", response.FactorResult, response.FactorResultMessage);
            }
        }
        protected void btnVerifySms_Click(object sender, EventArgs e)
        {
            string            factorId      = this.tbFactorID.Value;
            string            passcode      = this.tbSmsCode.Text;
            string            userId        = this.Master.Profile.Id;
            User              user          = Factory.UserClient.Get(userId);
            UserFactorsClient factorsClient = Factory.UserClient.GetUserFactorsClient(user);
            Factor            factor        = factorsClient.GetFactor(factorId);
            Factor            response      = factorsClient.Activate(factor, passcode);

            if (response.Status == "ACTIVE")
            {
                this.tbSmsCode.Visible    = false;
                this.btnVerifySms.Visible = false;
                this.btnCancel.Visible    = false;
                this.btnDone.Visible      = true;
                lblMessage.Text           = "Your phone number has been successfully enrolled";
            }
            else
            {
                // TODO what to do if the verification fails?
                //lblMessage.Text = string.Format("Result {0}: {1}", response.FactorResult, response.FactorResultMessage);
            }
        }