/// <summary> /// 删除用户 /// </summary> /// <param name="userID">用户编号</param> public HttpResponseMessage Delete(int userID) { string tokenID = string.Empty; var queryString = Request.GetQueryNameValuePairs(); var token = queryString.FirstOrDefault(q => q.Key.ToLower() == "token"); if (!string.IsNullOrEmpty(token.Value)) { tokenID = token.Value; } AuthenticationFunction.TokenValidation(tokenID); if (userID == 0) { return new HttpResponseMessage(HttpStatusCode.BadRequest); } else { var user = new Database.User() { UserID = userID }; var userFunction = new UserFunction(); int error = userFunction.DeleteUser(user); var response = GetResponse.UserResponse(error); return response; } }
/// <summary> /// 删除用户 /// </summary> /// <param name="entity">用户视图</param> public HttpResponseMessage Delete([FromBody]User entity) { string tokenID = string.Empty; var queryString = Request.GetQueryNameValuePairs(); var token = queryString.FirstOrDefault(q => q.Key.ToLower() == "token"); if (!string.IsNullOrEmpty(token.Value)) { tokenID = token.Value; } AuthenticationFunction.TokenValidation(tokenID); if (!AuthenticationFunction.IsAuthenticated) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "token expired")); } if (entity == null) { return new HttpResponseMessage(HttpStatusCode.BadRequest); } else { var userFunction = new UserFunction(); int error = userFunction.DeleteUser(entity); var response = GetResponse.UserResponse(error); return response; } }
/// <summary> /// 查询用户或用户登录 /// </summary> /// <returns>用户视图,总页数</returns> public HttpResponseMessage Get() { int totalPage = 0; string tokenID = string.Empty; List<UserViewResult> result = new List<UserViewResult>(); var queryString = Request.GetQueryNameValuePairs(); var token = queryString.FirstOrDefault(q=>q.Key.ToLower() == "token"); if (!string.IsNullOrEmpty(token.Value)) { tokenID = token.Value; } if (queryString.Any(q => q.Key.ToLower() == "logout")) { TokenFunction tokenFunction = new TokenFunction(); tokenFunction.DeleteToken(new Token() { TokenID = tokenID }); return Request.CreateResponse(HttpStatusCode.OK); } else if (queryString.Any(q => q.Key.ToLower() == "password")) { string userName = queryString.FirstOrDefault(q => q.Key.ToLower() == "username").Value; string password = queryString.FirstOrDefault(q => q.Key.ToLower() == "password").Value; var logonUser = new UserViewResult(); int error = AuthenticationFunction.UserLogon(userName, password, out tokenID, out logonUser); if (error == 0) { List<object> objectResult = new List<object>() { logonUser, tokenID }; return Request.CreateResponse(HttpStatusCode.OK, objectResult); } else { return GetResponse.UserResponse(error); } } else { AuthenticationFunction.TokenValidation(tokenID); if (!AuthenticationFunction.IsAuthenticated) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "token expired")); } var queryConditions = new UserQueryConditions(); queryConditions.GetValues(queryString); UserFunction userFunction = new UserFunction(); userFunction.QueryUsers(queryConditions, out result, out totalPage); List<object> objectResult = new List<object>() { result, new { totalPage = totalPage } }; return Request.CreateResponse(HttpStatusCode.OK, objectResult); } }
/// <summary> /// 查询单个用户 /// </summary> /// <param name="userID">用户编号</param> /// <returns>用户视图</returns> public UserViewResult Get(int userID) { int totalPage = 0; List<UserViewResult> result; string tokenID = string.Empty; var queryString = Request.GetQueryNameValuePairs(); var token = queryString.FirstOrDefault(q => q.Key.ToLower() == "token"); if (!string.IsNullOrEmpty(token.Value)) { tokenID = token.Value; } AuthenticationFunction.TokenValidation(tokenID); if (!AuthenticationFunction.IsAuthenticated) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "token expired")); } UserQueryConditions queryConditions = new UserQueryConditions() { UserID = userID }; UserFunction userFunction = new UserFunction(); userFunction.QueryUsers(queryConditions, out result, out totalPage); return result.FirstOrDefault(); }
//用户登录验证和发放令牌 public static int UserLogon(string userName, string password, out string tokenID, out UserViewResult logonUser) { tokenID = string.Empty; logonUser = new UserViewResult(); Database.User entity = new Database.User() { UserName = userName, Password = password }; UserFunction userFunction = new UserFunction(); int error = userFunction.CheckUser(entity, out logonUser); if (error != 0) { return error; } else { tokenID = AuthenticationFunction.CreateTokenID(logonUser); var newToken = new Token() { TokenID = tokenID, UserID = logonUser.UserID, ExpiredTime = DateTime.Now.AddMinutes(60) }; TokenFunction tokenFunction = new TokenFunction(); tokenFunction.AddToken(newToken); return 0; } }