//用户登录验证和发放令牌 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; } }
/// <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); } }
//令牌验证 public static View_TokenUser TokenValidation(string tokenID) { if (string.IsNullOrEmpty(tokenID)) { return null; } else { TokenFunction tokenFunction = new TokenFunction(); var tokenUser = tokenFunction.QueryTokenUser(tokenID); if (tokenUser == null) { return null; } else if (tokenUser.ExpiredTime < DateTime.Now) { tokenFunction.DeleteToken(new Token() { TokenID = tokenID }); return null; } else { tokenUser.ExpiredTime = DateTime.Now.AddMinutes(60); UserViewResult logonUser = new UserViewResult(); PropertyFunction.CopyEntity(tokenUser, logonUser); MyIdentity myIdentity = new MyIdentity(logonUser); MyPrincipal myPrincipal = new MyPrincipal(myIdentity); SetPrincipal(myPrincipal); Token newToken = new Token(); PropertyFunction.CopyEntity(tokenUser, newToken); tokenFunction.EditTokenByToken(newToken); return tokenUser; } } }