예제 #1
0
        public ActionResult AddUser(AspNetNewUserModel model)
        {
            if (WWIDal.UserNameExists(model.UserName))
            {
                return(RedirectToAction("Users", "Administration"));
            }

            HashingManager     hasher    = new HashingManager();
            WWIDal             dal       = new WWIDal();
            AspNetNewUserModel userModel = new AspNetNewUserModel();

            try
            {
                userModel = new AspNetNewUserModel()
                {
                    AccessFailedCount = 0,
                    Email             = model.Email,
                    EmailConfirmed    = model.EmailConfirmed,
                    Id                   = Guid.NewGuid().ToString(),
                    LockoutEnabled       = false,
                    LockoutEndDateUtc    = DateTime.MinValue,
                    PasswordHash         = hasher.HashToString(model.Password),
                    PhoneNumber          = (string.IsNullOrEmpty(model.PhoneNumber)) ? "" : model.PhoneNumber,
                    PhoneNumberConfirmed = model.PhoneNumberConfirmed,
                    TwoFactorEnabled     = model.TwoFactorEnabled,
                    UserName             = model.UserName,
                    cbAdministrator      = model.cbAdministrator,
                    cbExecutive          = model.cbExecutive,
                    cbInventory          = model.cbInventory,
                    cbSales              = model.cbSales,
                    cbSupplier           = model.cbSupplier,
                    cbUser               = model.cbUser,
                    cbVendor             = model.cbVendor
                };

                dal = new WWIDal();
                int ret = dal.AddUser(userModel);
            }
            catch (Exception ex)
            {
                Logger.LogError("AddUser Exception", ex);
            }

            return(RedirectToAction("Users", "Administration"));
        }
예제 #2
0
        public int AddUser(AspNetNewUserModel model)
        {
            int     _ret   = 0;
            string  userId = Guid.NewGuid().ToString();
            SQLData db     = new SQLData();
            string  sql    = @"INSERT INTO dbo.AspNetUsers (Id, Email, EmailConfirmed, PasswordHash, PhoneNumber, PhoneNumberConfirmed, 
														TwoFactorEnabled, LockoutEnabled, UserName, AccessFailedCount)
												VALUES(@Id, @Email, @EmailConfirmed, @PasswordHash, @PhoneNumber, @PhoneNumberConfirmed,
														@TwoFactorEnabled, @LockoutEnabled, @UserName, 0)"                                                        ;

            try
            {
                // Insert AspNetUsers table information.
                List <SqlParameter> parms = new List <SqlParameter>();
                parms.Add(new SqlParameter("@Id", userId));
                parms.Add(new SqlParameter("@Email", model.Email));
                parms.Add(new SqlParameter("@EmailConfirmed", model.EmailConfirmed));
                parms.Add(new SqlParameter("@PasswordHash", model.PasswordHash));
                parms.Add(new SqlParameter("@PhoneNumber", model.PhoneNumber));
                parms.Add(new SqlParameter("@PhoneNumberConfirmed", model.PhoneNumberConfirmed));
                parms.Add(new SqlParameter("@TwoFactorEnabled", model.TwoFactorEnabled));
                parms.Add(new SqlParameter("@LockoutEnabled", model.LockoutEnabled));
                parms.Add(new SqlParameter("@UserName", model.UserName));
                db.Open(ConfigurationManager.ConnectionStrings["WWI"].ConnectionString);
                db.ExecuteNonQuery(sql, parms);
                db.Close();

                // Insert User roles.
                if (model.cbAdministrator)
                {
                    InsertUserRole(userId, "Administrator");
                }
                if (model.cbContractor)
                {
                    InsertUserRole(userId, "Contractor");
                }
                if (model.cbExecutive)
                {
                    InsertUserRole(userId, "Executive");
                }
                if (model.cbInventory)
                {
                    InsertUserRole(userId, "Inventory");
                }
                if (model.cbSales)
                {
                    InsertUserRole(userId, "Sales");
                }
                if (model.cbSupplier)
                {
                    InsertUserRole(userId, "Supplier");
                }
                if (model.cbUser)
                {
                    InsertUserRole(userId, "User");
                }
                if (model.cbVendor)
                {
                    InsertUserRole(userId, "Vendor");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("WWIDal.AddUser Exception", ex);
            }

            return(_ret);
        }