CreateUser Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { // only first 3 fields are passed in from the AccountModels.cs try { status = UsernameExists(username); if (status == MembershipCreateStatus.DuplicateUserName) { return null; } status = DuplicateEmail(email); if (status == MembershipCreateStatus.DuplicateEmail) { return null; } byte[] hashedPassword = HashPassword(password.Trim()); GetValues gv = new GetValues(); string ipAddress = gv.getIPAddress(); int userStatus = Convert.ToInt32(SSS.GlobalListValues.Enums.UserStatusCode.Active); using (var context = new SSSEntities()) { UserProfile newUser = new UserProfile() { Email = email, UserPassword = hashedPassword, UserName = username, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, DatePasswordLastChanged = DateTime.Now, DateLastLogin = DateTime.Now, UserStatusCode = userStatus, IpAddress = ipAddress, }; // insert the User Role int userRole = Convert.ToInt32(SSS.GlobalListValues.Enums.UserRoleCode.User_Public); // look up the desired user role : // uses a UserRole join table with a many to many relation // between the UserProfile table and the ListValue table ListValue ur = context.ListValues .SingleOrDefault(lv => lv.ListValueId == userRole); newUser.UserProfileUserRoles.Add(ur); context.UserProfiles.AddObject(newUser); context.SaveChanges(); // NKT: after creation, go back and retrieve the auto-generated identity key and // update the userId's for the created and updated userId int userId = newUser.UserId; newUser.CreatedUserId = userId; newUser.UpdatedUserId = userId; context.SaveChanges(); status = MembershipCreateStatus.Success; return GetMembershipUser(newUser); } } catch (ArgumentException) { status = MembershipCreateStatus.ProviderError; return null; } }
[SIGNATURE]