添加项目文件。
This commit is contained in:
78
Szmedi.AIScriptRunner/Services/AuthenticationService.cs
Normal file
78
Szmedi.AIScriptRunner/Services/AuthenticationService.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
|
||||
using Flurl.Http;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using Szmedi.AIScriptRunner.Properties;
|
||||
using Szmedi.AIScriptRunner.Web;
|
||||
|
||||
namespace Szmedi.AIScriptRunner.Services
|
||||
{
|
||||
public class AuthenticationService
|
||||
{
|
||||
|
||||
private string GetStreamReader(string url, string postData, string auth = "", int timeout = 60000, string encode = "utf-8", string contentType = "application/json", string method = "POST")
|
||||
{
|
||||
var data = Encoding.GetEncoding(encode).GetBytes(postData);
|
||||
var webRequest = (HttpWebRequest)WebRequest.Create(url);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(auth))
|
||||
{
|
||||
var athu = "Authorization:" + auth;
|
||||
var whc = new WebHeaderCollection();
|
||||
whc.Add(athu);
|
||||
webRequest.Headers.Add(whc);
|
||||
}
|
||||
webRequest.Method = method;
|
||||
webRequest.ContentType = contentType + ";" + encode;
|
||||
webRequest.ContentLength = data.Length;
|
||||
webRequest.Timeout = timeout;
|
||||
var requestStream = webRequest.GetRequestStream();
|
||||
requestStream.Write(data, 0, data.Length);
|
||||
var webResponse = (HttpWebResponse)webRequest.GetResponse();
|
||||
var responseStream = webResponse.GetResponseStream();
|
||||
if (responseStream == null) { return string.Empty; }
|
||||
var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
|
||||
return streamReader.ReadToEnd();
|
||||
}
|
||||
|
||||
public async Task<ResponseVO> LoginAsync(string username, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 1. 取公钥
|
||||
var keyJson = await Settings.Default.PublicKeyUrl.GetStringAsync();
|
||||
var pubKeyVO = JsonConvert.DeserializeObject<PublicKeyVO>(keyJson);
|
||||
if (pubKeyVO?.Successful == true)
|
||||
{
|
||||
// 2. 用 BouncyCastle 加密
|
||||
var encrypted = RSAHelper.EncryptByJavaKey(pubKeyVO.Data.PublicKey, $"{password}");
|
||||
|
||||
var jsonData = $"{{\"accountName\":\"{username}\",\"password\":\"{encrypted}\",\"uuid\":\"{pubKeyVO.Data.UUID}\",\"appVersion\":\"newversion\"}}";
|
||||
// 3. 发送登录请求
|
||||
var respones = GetStreamReader(Settings.Default.LoginUrl, jsonData);
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||
};
|
||||
|
||||
return JsonConvert.DeserializeObject<ResponseVO>(respones, settings);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("获取不到用户公钥信息,请联系管理员");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user