Пример #1
0
        public ActionResult Create(CSPersonnel cspersonnel)
        {
            if (ModelState.IsValid)
            {
                // I would really like a transaction here....
                CSPersonnel cp = new CSPersonnel();

                cp.firstName = cspersonnel.firstName;
                cp.lastName = cspersonnel.lastName;
                cp.middleName = cspersonnel.middleName;
                cp.isActive = cspersonnel.isActive;
                cp.lastAccess = DateTime.Now;

                try
                {

                    if (WebSecurity.Initialized == false)
                    {
                        // WebSecurity is used to create the new user and account.
                        WebSecurity.InitializeDatabaseConnection("DefaultConnection",
                       "UserProfile", "UserId", "UserName", autoCreateTables: false);
                    }

                    string password = Membership.GeneratePassword(12, 1);

                    // Create both the user and account.
                    WebSecurity.CreateUserAndAccount(cspersonnel.UserProfile.UserName, password, new { email = cspersonnel.UserProfile.Email });

                    // Assign a user to a role.
                    Roles.AddUserToRole(cspersonnel.UserProfile.UserName, "Personnel");

                    // Update the foreign key in cp
                    cp.UserId = (int)Membership.GetUser(cspersonnel.UserProfile.UserName).ProviderUserKey;

                    // save cspersonnel to db
                    db.CSPersonnels.Add(cp);
                    db.SaveChanges();

                    CIOS.Email.EmailSystem email = new CIOS.Email.EmailSystem();

                    email.toEmail = cspersonnel.UserProfile.Email;
                    email.subject = "CIOS: New Account Created";
                    email.body = "You can log into your account with the following information:\n" +
                        "Username: "******"\n" +
                        "Password: "******"\n\n" +
                        "Please log into the CIOS system and change your password.";

                    email.sendNewEmail();

                    return RedirectToAction("Index");

                } catch (System.Web.Security.MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", "The username already exists.");
                }
            }

               // ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName", cspersonnel.UserId);
            return View(cspersonnel);
        }
Пример #2
0
 public ActionResult Edit(CSPersonnel cspersonnel)
 {
     if (ModelState.IsValid)
     {
         db.Entry(cspersonnel).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     //ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName", cspersonnel.UserId);
     return View(cspersonnel);
 }