/// <summary>
        /// This method fires when the user sends a confirmation email
        /// </summary>
        /// <param name="sender">The lbSendConfirmEmail LinkButton</param>
        /// <param name="e">The Click event</param>
        protected void lbSendConfirmEmail_Click(object sender, EventArgs e)
        {
            //The user object
            PyramidUser user = null;

            //Get the user object
            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                user = context.Users.Where(u => u.Id == hfUserPK.Value).FirstOrDefault();
            }

            //Make sure the user exists
            if (user != null && user.Id != null)
            {
                //Get the user manager
                var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();

                //If the user exists, send the user an email to confirm their account
                string emailcode   = manager.GenerateEmailConfirmationToken(user.Id);
                string callbackUrl = IdentityHelper.GetAccountConfirmationRedirectUrl(emailcode, user.Id, Request);
                manager.SendEmail(user.Id, "Confirm your account", Utilities.GetEmailHTML(callbackUrl, "Confirm Account", true, "Welcome " + user.FirstName + " " + user.LastName + "!", "Your user account for the Pyramid Model Implementation Data System was created by an administrator.<br/>Your username for this system is:<br/><br/>" + user.UserName + "<br/><br/>Once you confirm your account and create your password, you will be able to start using the system.<br/>To get started, please click the link below.", Request));

                //Show the user a success message
                msgSys.ShowMessageToUser("success", "Email Sent", "Confirmation email successfully sent!", 5000);
            }
            else
            {
                //Show an error message
                msgSys.ShowMessageToUser("danger", "Error", "The user could not be found!", 10000);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method fires when the user clicks the save button and
        /// it attempts to add a new user to the system with the information
        /// provided on the page
        /// </summary>
        /// <param name="sender">The submitUser control</param>
        /// <param name="e">The Click event</param>
        protected void submitUser_Click(object sender, EventArgs e)
        {
            //Get the user manager
            var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();

            //Create and fill the user object
            PyramidUser newUser = new PyramidUser();

            newUser.FirstName            = txtFirstName.Value.ToString();
            newUser.LastName             = txtLastName.Value.ToString();
            newUser.UserName             = txtUsername.Value.ToString();
            newUser.Email                = txtEmail.Value.ToString();
            newUser.EmailConfirmed       = false;
            newUser.TwoFactorEnabled     = false;
            newUser.PhoneNumber          = (txtPhoneNumber.Value == null ? null : txtPhoneNumber.Value.ToString());
            newUser.PhoneNumberConfirmed = false;

            //Attempt to create the user
            IdentityResult result = manager.Create(newUser, txtPassword.Value.ToString());

            if (result.Succeeded)
            {
                //If the user creation succeeded, send the user an email to confirm their account
                string emailcode   = manager.GenerateEmailConfirmationToken(newUser.Id);
                string callbackUrl = IdentityHelper.GetAccountConfirmationRedirectUrl(emailcode, newUser.Id, Request);
                manager.SendEmail(newUser.Id, "Confirm your account", Utilities.GetEmailHTML(callbackUrl, "Confirm Account", true, "Welcome " + newUser.FirstName + " " + newUser.LastName + "!", "Your user account for the Pyramid Model Implementation Data System was created by an administrator.<br/>Your username for this system is:<br/><br/>" + newUser.UserName + "<br/><br/>Once you confirm your account and create your password, you will be able to start using the system.<br/>To get started, please click the link below.", Request));

                //Add the user to their identity role
                manager.AddToRole(newUser.Id, ddIdentityRole.SelectedItem.Text.ToString());

                //Add the user to their program role
                using (PyramidContext context = new PyramidContext())
                {
                    //Create the UserProgramRole object and fill it
                    UserProgramRole userPrgRole = new UserProgramRole();
                    userPrgRole.CreateDate        = DateTime.Now;
                    userPrgRole.Creator           = User.Identity.Name;
                    userPrgRole.ProgramFK         = Convert.ToInt32(ddProgram.Value);
                    userPrgRole.ProgramRoleCodeFK = Convert.ToInt32(ddProgramRole.Value);
                    userPrgRole.Username          = newUser.UserName;

                    //Add the UserProgramRole to the database and save
                    context.UserProgramRole.Add(userPrgRole);
                    context.SaveChanges();
                }

                //Redirect the user
                Response.Redirect("/Admin/UserManagement?message=CreateUserSuccess");
            }
            else
            {
                msgSys.ShowMessageToUser("danger", "Error", result.Errors.FirstOrDefault(), 120000);
            }
        }