133 lines
5.2 KiB
C#
133 lines
5.2 KiB
C#
using OpenAuth.App.BaseApp.Base;
|
|
using OpenAuth.Repository.Domain;
|
|
using OpenAuth.Repository;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Infrastructure.CloudSdk.minio;
|
|
using OpenAuth.App.BasicQueryService;
|
|
using OpenAuth.App.Interface;
|
|
using OpenAuth.WebApi;
|
|
using SqlSugar;
|
|
using Infrastructure;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
using Infrastructure.Helpers;
|
|
using System.Text.Json;
|
|
using NetModular.DocX.Core;
|
|
using System.DirectoryServices.Protocols;
|
|
using OpenAuth.App.ServiceApp.Response;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
namespace OpenAuth.App.ServiceApp
|
|
{
|
|
public class TestApp : SqlSugarBaseApp<LasaDronePort, SugarDbContext>
|
|
{
|
|
private EncryptionHelper _helper;
|
|
public TestApp(EncryptionHelper helper, ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<LasaDronePort> repository, IAuth auth): base(unitWork, repository, auth)
|
|
{
|
|
_helper = helper;
|
|
}
|
|
|
|
public async Task<Response<string>> RegistService()
|
|
{
|
|
//using var httpClient = new HttpClient();
|
|
Response<string> Response=new Response<string>();
|
|
|
|
var handler = new HttpClientHandler();
|
|
//handler.ClientCertificates.Add(cert);
|
|
// 如果需要忽略服务器证书错误(仅测试环境)
|
|
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
|
|
using (var client = new HttpClient())
|
|
{
|
|
try
|
|
{
|
|
string url = "https://wp.tianmu.cloud:5443/zhcfzx/droneAirport/RegistService";
|
|
// var param = new Dictionary<string, string>()
|
|
//{
|
|
// { "serviceUrl", "https://xxx.com/mdc/v1/" },
|
|
// { "1iveStreampluginUrl", "https://ss.com/drone/v1/livestream.js" },
|
|
// { "contacts", "孟三" },
|
|
// { "phone", "18390849120" },
|
|
|
|
//};
|
|
string json= "{" +
|
|
"\"serviceUrl\": \"https://xxx.com/mdc/v1/\"," +
|
|
"\"liveStreampluginUrl\": \"https://xxx.com/drone/v1/livestream.js\"," +
|
|
"\"contacts\": \"孟三\"," +
|
|
"\"phone\": \"18390849120\"" +
|
|
"}";
|
|
string x_lc_secret = _helper.getxseret();
|
|
string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
|
|
string x_token = _helper.GetToken(centercode);
|
|
|
|
// 序列化为 JSON 字符串
|
|
//string json = JsonSerializer.Serialize(param);
|
|
|
|
// 转换为字节数组
|
|
byte[] bytes = Encoding.UTF8.GetBytes(json);
|
|
|
|
var encryptedData = _helper.Encrypt(bytes);
|
|
// 创建请求体
|
|
var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");
|
|
|
|
// 添加请求头
|
|
client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
|
|
client.DefaultRequestHeaders.Add("x-lc-token", x_token);
|
|
|
|
//发送请求
|
|
HttpResponseMessage response = await client.PostAsync(url, httpContent);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
// 获取响应头中的 x_cl_screte 参数
|
|
string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");
|
|
|
|
// 读取响应内容并反序列化
|
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);
|
|
|
|
//解密数据
|
|
byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
|
|
byte[] resdatabytes = Convert.FromBase64String(result?.data);
|
|
string data = _helper.Decrypt(resbytesx, resdatabytes);
|
|
|
|
Response.Result = data;
|
|
Response.Message = result.message;
|
|
Response.Result = result.code.ToString();
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
Console.WriteLine("\nException Caught!");
|
|
Console.WriteLine("Message :{0} ", e.Message);
|
|
Response.Result = "连接错误";
|
|
}
|
|
}
|
|
|
|
return Response;
|
|
}
|
|
|
|
|
|
// 安全获取响应头值的方法
|
|
private static string GetHeaderValue(HttpResponseMessage response, string headerName)
|
|
{
|
|
if (response.Headers.TryGetValues(headerName, out IEnumerable<string> values))
|
|
{
|
|
return string.Join(", ", values); // 如果多个值则合并
|
|
}
|
|
|
|
// 尝试忽略大小写再次查找
|
|
foreach (var header in response.Headers)
|
|
{
|
|
if (header.Key.Equals(headerName, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return string.Join(", ", header.Value);
|
|
}
|
|
}
|
|
|
|
return null; // 未找到
|
|
}
|
|
}
|
|
}
|