public PartialViewResult MigrateUser(string id)
        {
            var legacyUser = LegacyUsers.FirstOrDefault(c => c.User == id);

            if (legacyUser != null && MembershipUsers.All(c => c.UserName != legacyUser.User))
            {
                try
                {
                    double hourlyRate;
                    MembershipCreateStatus createStatus;
                    var membershipUser = MembershipUserExtended.CreateUser(legacyUser.User, legacyUser.Pwd,
                                                                           legacyUser.User + "@mmc.com",
                                                                           legacyUser.Firstname, legacyUser.Lastname,
                                                                           string.Empty,
                                                                           !double.TryParse(legacyUser.Hourly_rate,
                                                                                            out hourlyRate)
                                                                               ? 0.0
                                                                               : hourlyRate,
                                                                           legacyUser.Address, string.Empty,
                                                                           string.Empty,
                                                                           string.Empty, legacyUser.Phonenumber,
                                                                           out createStatus);
                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        _membershipUsers = GetAllMemberShipUsers();
                        if (!string.IsNullOrEmpty(legacyUser.Role) && Roles.GetAllRoles().Contains(legacyUser.Role))
                        {
                            Roles.AddUserToRole(membershipUser.UserName, legacyUser.Role);
                        }
                        else
                        {
                            Roles.AddUserToRole(membershipUser.UserName,
                                                Roles.GetAllRoles().FirstOrDefault(c => c.ToLower().Equals("user")));
                        }

                        MigrateUserData(membershipUser.UserName);

                        ViewBag.Message = string.Format("User {0} migrated successfully.", id);
                        return(PartialView(viewName: "_LegacyUserList", model: GetLegacyUsers()));
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.Message = string.Format("{0} error occured while migrating user {1}.", ex.Message.ToString(),
                                                    id);
                    return(PartialView("_LegacyUserList", GetLegacyUsers()));
                }
            }

            ViewBag.Message = string.Format("Error occured while migrating user {0}.", id);
            return(PartialView("_LegacyUserList", GetLegacyUsers()));
        }
示例#2
0
        public ActionResult Create(CreateUserRoleViewModel userRoleModel)
        {
            var model         = userRoleModel.UserModel;
            var selectedRoles = userRoleModel.Roles;

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                var user = MembershipUserExtended.CreateUser(model.UserName, model.Password, model.Email, model.FirstName, model.LastName, model.Title, model.HourlyRate, model.Address, model.City, model.State, model.Zip, model.Phone, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    //Assign Users to Roles
                    if (selectedRoles.Any())
                    {
                        Roles.AddUserToRoles(user.UserName, selectedRoles.ToArray());
                    }
                    else
                    {
                        Roles.AddUserToRole(user.UserName, Roles.GetAllRoles().FirstOrDefault(c => c.ToLower().Equals("user")));
                    }


                    userRoleModel.UserModel = new CreateUserModel(user);
                    userRoleModel.Roles     = Roles.GetAllRoles().ToList();
                    //FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    //return RedirectToAction("Index", "TimeTrack");
                    ModelState.AddModelError("", string.Format("User {0} created successfully", model.UserName));
                    return(View(userRoleModel));
                }
                else
                {
                    userRoleModel.Roles = Roles.GetAllRoles().ToList();
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }
            else
            {
                userRoleModel.Roles = Roles.GetAllRoles().ToList();
            }

            // If we got this far, something failed, redisplay form
            return(View(userRoleModel));
        }