public string Create(User user) { try { // Password要MD5加密 user.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(user.Password, "MD5"); // 检验是否已经存在该用户 var ls = this.GetUserList().Where(a => a.Username == user.Username); if (ls.Count() > 0) { return "<p class='alert alert-danger'>该用户名已存在,无法添加!</p>"; } dbContext.Users.Add(user); dbContext.SaveChanges(); // 更新内存 this.Update(1); return "<p class='alert alert-success'>新增用户成功 <a href='/User/Index'>返回</a></p>"; } catch (Exception e1) { return "<p class='alert alert-danger'>出错了: " + e1.Message + "</p>"; } }
public string InitUser() { var ls = this.GetUserList(); if(ls.Count > 0) { return "System has Users, you cannot init it again."; } User u = new User() { Username = "******", Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5"), UserType = 0, IsAdmin = true }; dbContext.Users.Add(u); dbContext.SaveChanges(); this.Update(1); return "Init Completed. User 'Admin/123456' has been created."; }
public string Edit(User user) { try { dbContext.Entry(user).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); // 更新内存 this.Update(1); } catch (Exception e1) { return "<p class='alert alert-danger'>出错了: " + e1.Message + "</p>"; } return "<p class='alert alert-success'>修改成功</p>"; }