public CreateUserStatus CreateUser(RegistrationData user, [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof (ValidationErrorResources))] [StringLength(50, MinimumLength = 1, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof (ValidationErrorResources))] string password) { if (user == null) { throw new ArgumentNullException("user"); } // Run this BEFORE creating the user to make sure roles are enabled and the default role // will be available // // If there are a problem with the role manager it is better to fail now than to have it // happening after the user is created if (!Roles.RoleExists(DefaultRole)) { Roles.CreateRole(DefaultRole); } // NOTE: ASP.NET by default uses SQL Server Express to create the user database. // CreateUser will fail if you do not have SQL Server Express installed. MembershipCreateStatus createStatus; Membership.CreateUser(user.UserName, password, user.Email, user.Question, user.Answer, true, null, out createStatus); if (createStatus != MembershipCreateStatus.Success) { return ConvertStatus(createStatus); } var model = new MRZSEntities(); aspnet_Users createdUser = model.aspnet_Users.SingleOrDefault(u => u.UserName == user.UserName); var adUser = new Models.User(); adUser.FirstName = user.FirstName; adUser.LastName = user.LastName; adUser.aspnet_UserId = createdUser.UserId; if (user.GroupId > 0) { adUser.GroupId = user.GroupId; } model.Users.AddObject(adUser); model.SaveChanges(); // Assign it to the default role // This *can* fail but only if role management is disabled if (!string.IsNullOrEmpty(user.UserRole)) Roles.AddUserToRole(user.UserName, user.UserRole); // Set its friendly name (profile setting) // This *can* fail but only if Web.config is configured incorrectly ProfileBase profile = ProfileBase.Create(user.UserName, true); profile.SetPropertyValue("FriendlyName", user.FriendlyName); profile.Save(); return CreateUserStatus.Success; }
public void EditUser(RegistrationData user, [StringLength(50, MinimumLength = 1, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof (ValidationErrorResources))] string password) { if (user == null) { throw new ArgumentNullException("user"); } var model = new MRZSEntities(); Models.User createdUser = model.Users.SingleOrDefault(u => u.UserId == user.UserId); createdUser.FirstName = user.FirstName; createdUser.LastName = user.LastName; if (user.GroupId > 0) { createdUser.GroupId = user.GroupId; } model.SaveChanges(); if (!string.IsNullOrEmpty(user.UserRole)) { if (Roles.IsUserInRole(user.UserName, user.UserRole)) return; string[] userRoles = Roles.GetRolesForUser(user.UserName); if (userRoles.Length > 0) Roles.RemoveUserFromRoles(user.UserName, userRoles); Roles.AddUserToRole(user.UserName, user.UserRole); } }