//********************************************************** //* Создание пользователя //********************************************************** public static MembershipUser CreateUser(String username, String password, Int32 roleId) { using (SandBoxDataContext db = new SandBoxDataContext()) { var users = UserManager.GetUsers(); if (Enumerable.Any(users, us => us.Login == username)) return null; User user = new User { Login = username, Password = password, PasswordSalt = CreateSalt(), CreatedDate = DateTime.Now, LastLoginDate = DateTime.Now }; user.Password = CreatePasswordHash(password, user.PasswordSalt); db.Users.InsertOnSubmit(user); db.SubmitChanges(); var usr = db.Users.FirstOrDefault(x => x.Login == username); if (usr == null) return null; UsersInRole userInRole = new UsersInRole {UserId = usr.UserId, RoleId = roleId}; db.UsersInRoles.InsertOnSubmit(userInRole); db.SubmitChanges(); return GetUser(username); } }
public static void EditUser(Int32 userid, String username, String login, String password, Int32 roleId, Int32 roleId2) { using (SandBoxDataContext db = new SandBoxDataContext()) { User user = db.Users.FirstOrDefault(x => x.UserId == userid); if (user == null) return; user.Login = login; user.UserName = username; if (password != String.Empty) { user.PasswordSalt = CreateSalt(); user.Password = CreatePasswordHash(password, user.PasswordSalt); } var userinroles = from ur in db.UsersInRoles where ur.UserId == user.UserId select ur; foreach (UsersInRole userinrole in userinroles) db.UsersInRoles.DeleteOnSubmit(userinrole); UsersInRole userInRole = new UsersInRole { UserId = user.UserId, RoleId = roleId }; db.UsersInRoles.InsertOnSubmit(userInRole); UsersInRole userInRole2 = new UsersInRole { UserId = user.UserId, RoleId = roleId2 }; db.UsersInRoles.InsertOnSubmit(userInRole2); db.SubmitChanges(); } }
partial void UpdateUsersInRole(UsersInRole instance);
partial void DeleteUsersInRole(UsersInRole instance);
partial void InsertUsersInRole(UsersInRole instance);