//创建用户 public int CreateUser(SystemUser user, List <UserRoleRel> roles, List <UserOrgRel> orgs) { if (user == null || string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.UserPassword)) { return(-2); } user.UserPassword = Md5Encrypt.GetMd5Hash(user.UserPassword); if (SqlHelper.Insert(user) > 0) { foreach (var role in roles) { role.UserId = user.UserId; SqlHelper.Insert(role); } if (orgs != null) { foreach (var org in orgs) { org.UserId = user.UserId; SqlHelper.Insert(org); } } return(1); } return(-1); }
public IHttpActionResult Authenticate(LoginRequest login) { if (login == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } Usuario usuario = null; usuario = db.Usuario.Where(x => x.NombreUsuario == login.username).First(); if (usuario == null) { return(NotFound()); } using (MD5 md5Hash = MD5.Create()) { var md5 = new Md5Encrypt(); string contraseña = md5.GetMd5Hash(md5Hash, login.password); bool isCredentialValid = (contraseña.ToUpper() == usuario.Contraseña); if (isCredentialValid) { var token = TokenGenerator.GenerateTokenJwt(login.username); return(Ok(token)); } else { return(Unauthorized()); } } }
//登陆 public SystemUser Login(string userName, string password) { if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) { return(null); } IBaseEntity entity = new SystemUser(); SqlHelper.GetSingleEntity(userName, ref entity); var user = (SystemUser)entity; if (userName.Equals(user.UserName, StringComparison.CurrentCulture) && Md5Encrypt.GetMd5Hash(password).Equals(user.UserPassword, StringComparison.CurrentCulture)) { return(user); } return(null); }
//更新用户 public int UpdateUser(SystemUser user, List <UserRoleRel> roles, List <UserOrgRel> orgs) { if (user == null) { return(-2); } var oldUser = GetUser(user.UserId); if (oldUser == null) { return(-3); } if (oldUser.UserPassword != user.UserPassword) { user.UserPassword = Md5Encrypt.GetMd5Hash(user.UserPassword); } if (roles.Count > 0 && SqlHelper.Update(user) > 0) { SqlHelper.DeleteByField <UserRoleRel>("UserId", user.UserId); SqlHelper.DeleteByField <UserOrgRel>("UserId", user.UserId); foreach (var role in roles) { role.UserId = user.UserId; SqlHelper.Insert(role); } if (orgs != null) { foreach (var org in orgs) { org.UserId = user.UserId; SqlHelper.Insert(org); } } return(1); } return(-1); }