using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace WebApplication1
{
public class Test
{
private static readonly Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
private const string appId = "62acf88c-55d0-465c-b08d-99cd36271231";
public static Dictionary<string, string> testRegist(string address)
{
//RSA rsa = new RSA();
//String publicKey = rsa.getPublicKeyBase64();
//String privateKey = rsa.getPrivateKeyBase64();
//keyValuePairs.Add("LOCAL_PRIVATE_KEY", privateKey);
// keyValuePairs.Add("LOCAL_PUBLIC_KEY", publicKey);
//String data = HttpRequest.post(address + "/api/ec/dev/auth/regist")
// .header("appid", APPID)
// .header("cpk", publicKey)
// .timeout(2000)
// .execute().body();
//Dictionary<string, string> datas = JsonConvert.SerializeObject(data);
//keyValuePairs.Add("SERVER_PUBLIC_KEY", datas.spk);
//keyValuePairs.Add("SERVER_SECRET", datas.secrit);
// return datas;
return null;
}
public static Dictionary<string, string> testGetoken(string address)
{
// 从系统缓存或者数据库中获取ECOLOGY系统公钥和Secret信息
string secret = keyValuePairs["SERVER_SECRET"];
string spk = keyValuePairs["SERVER_PUBLIC_KEY"];
// 如果为空,说明还未进行注册,调用注册接口进行注册认证与数据更新
if (string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(spk))
{
testRegist(address);
// 重新获取最新ECOLOGY系统公钥和Secret信息
secret = keyValuePairs["SERVER_SECRET"];
spk = keyValuePairs["SERVER_PUBLIC_KEY"];
}
// 公钥加密,所以RSA对象私钥为null
//RSA rsa = new RSA(null, spk);
//对秘钥进行加密传输,防止篡改数据
//String encryptSecret = rsa.encryptBase64(secret, CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
//调用ECOLOGY系统接口进行注册
//String data = HttpRequest.post(address + "/api/ec/dev/auth/applytoken")
// .header("appid", APPID)
// .header("secret", encryptSecret)
// .header("time", "3600")
// .execute().body();
//Dictionary<string, string> datas = JsonConvert.SerializeObject(data);
//ECOLOGY返回的token
// TODO 为Token缓存设置过期时间
//keyValuePairs.Add("SERVER_TOKEN", datas["token"]);
//return datas;
return null;
}
public static string testRestful(String address, String api, String jsonParams)
{
//ECOLOGY返回的token
string token = keyValuePairs["SERVER_TOKEN"];
if (string.IsNullOrEmpty(token))
{
token = testGetoken(address)["token"];
}
string spk = keyValuePairs["SERVER_PUBLIC_KEY"];
//封装请求头参数
//RSA rsa = new RSA(null, spk);
//对用户信息进行加密传输,暂仅支持传输OA用户ID
//String encryptUserid = rsa.encryptBase64("1", CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
//调用ECOLOGY系统接口,注意此处的disableCookie,可翻阅hutool的文档查看
//String data = HttpRequest.disableCookie()
// .get(address + api)
// .header("appid", APPID)
// .header("token", token)
// .header("userid", encryptUserid)
// .body(jsonParams)
// .execute().body();
//return data;
return "";
}
}
}