基本完成登陆验证 同一JSESSIONID多次登录导致需要验证码??

This commit is contained in:
lichx 2024-03-14 00:02:17 +08:00
parent 566ad86d4a
commit c55a712eca
13 changed files with 275 additions and 68 deletions

View File

@ -22,6 +22,7 @@
<ItemGroup> <ItemGroup>
<None Remove="AppSettings.json" /> <None Remove="AppSettings.json" />
<None Remove="AppSettings_Debug.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -40,6 +41,9 @@
<Content Include="AppSettings.json"> <Content Include="AppSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Include="AppSettings_Debug.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -57,4 +61,6 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ProjectExtensions><VisualStudio><UserProperties appsettings_3debug_1json__JsonSchema="https://json.schemastore.org/appsettings.json" /></VisualStudio></ProjectExtensions>
</Project> </Project>

View File

@ -14,7 +14,6 @@ using System.Windows.Forms;
namespace CSS_Solution.Forms namespace CSS_Solution.Forms
{ {
partial class MainForm : Form partial class MainForm : Form
{ {
public MainForm() public MainForm()

View File

@ -28,14 +28,17 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container();
Welcome = new Label(); Welcome = new Label();
password = new TextBox(); password = new TextBox();
ID = new TextBox(); ID = new TextBox();
MainPanel = new Panel(); MainPanel = new Panel();
info = new Label();
countdown = new Countdown(); countdown = new Countdown();
test = new Button(); test = new Button();
Record = new Button(); record = new Button();
SavePassword = new CheckBox(); SavePassword = new CheckBox();
errorInfo_CountDown = new System.Windows.Forms.Timer(components);
MainPanel.SuspendLayout(); MainPanel.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
@ -80,9 +83,10 @@
// //
// MainPanel // MainPanel
// //
MainPanel.Controls.Add(info);
MainPanel.Controls.Add(countdown); MainPanel.Controls.Add(countdown);
MainPanel.Controls.Add(test); MainPanel.Controls.Add(test);
MainPanel.Controls.Add(Record); MainPanel.Controls.Add(record);
MainPanel.Controls.Add(SavePassword); MainPanel.Controls.Add(SavePassword);
MainPanel.Controls.Add(password); MainPanel.Controls.Add(password);
MainPanel.Controls.Add(Welcome); MainPanel.Controls.Add(Welcome);
@ -93,6 +97,17 @@
MainPanel.Size = new Size(1403, 736); MainPanel.Size = new Size(1403, 736);
MainPanel.TabIndex = 3; MainPanel.TabIndex = 3;
// //
// info
//
info.AutoSize = true;
info.ForeColor = Color.White;
info.Location = new Point(86, 450);
info.Name = "info";
info.Size = new Size(91, 24);
info.TabIndex = 7;
info.Text = "SomeInfo";
info.Visible = false;
//
// countdown // countdown
// //
countdown.BackColor = Color.FromArgb(25, 25, 25); countdown.BackColor = Color.FromArgb(25, 25, 25);
@ -111,17 +126,19 @@
test.TabIndex = 5; test.TabIndex = 5;
test.Text = "验证密码"; test.Text = "验证密码";
test.UseVisualStyleBackColor = false; test.UseVisualStyleBackColor = false;
test.Click += test_Click;
// //
// Record // record
// //
Record.BackColor = Color.FromArgb(25, 25, 25); record.BackColor = Color.FromArgb(25, 25, 25);
Record.ForeColor = Color.White; record.ForeColor = Color.White;
Record.Location = new Point(86, 393); record.Location = new Point(86, 393);
Record.Name = "Record"; record.Name = "record";
Record.Size = new Size(112, 34); record.Size = new Size(112, 34);
Record.TabIndex = 4; record.TabIndex = 4;
Record.Text = "保存密码"; record.Text = "保存密码";
Record.UseVisualStyleBackColor = false; record.UseVisualStyleBackColor = false;
record.Click += record_Click;
// //
// SavePassword // SavePassword
// //
@ -135,6 +152,11 @@
SavePassword.UseVisualStyleBackColor = true; SavePassword.UseVisualStyleBackColor = true;
SavePassword.CheckedChanged += SavePassword_CheckedChanged; SavePassword.CheckedChanged += SavePassword_CheckedChanged;
// //
// errorInfo_CountDown
//
errorInfo_CountDown.Interval = 5000;
errorInfo_CountDown.Tick += errorInfo_CountDown_Tick;
//
// Welcome_Form // Welcome_Form
// //
AutoScaleDimensions = new SizeF(11F, 24F); AutoScaleDimensions = new SizeF(11F, 24F);
@ -159,8 +181,10 @@
private Panel MainPanel; private Panel MainPanel;
private FontDialog fontDialog1; private FontDialog fontDialog1;
private CheckBox SavePassword; private CheckBox SavePassword;
private Button Record; private Button record;
private Button test; private Button test;
private Countdown countdown; private Countdown countdown;
private Label info;
private System.Windows.Forms.Timer errorInfo_CountDown;
} }
} }

View File

@ -1,13 +1,17 @@
using Microsoft.Extensions.Logging; using CSS_Solution.Request;
using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Xml.Serialization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace CSS_Solution.Forms namespace CSS_Solution.Forms
{ {
@ -16,9 +20,17 @@ namespace CSS_Solution.Forms
public Welcome_Form() public Welcome_Form()
{ {
InitializeComponent(); InitializeComponent();
if (Program.exceptionHandler == null)
Initialize.default_logger.LogError($"[{DateTime.Now}] exceptionHandler为空どうやって");
else
Application.ThreadException += Program.exceptionHandler.ApplicationThreadExceptionHandler;
} }
private Font normal_font = new Font("Microsoft YaHei UI", 15F, FontStyle.Regular, GraphicsUnit.Point, 134); private Font normal_font = new Font("Microsoft YaHei UI", 15F, FontStyle.Regular, GraphicsUnit.Point, 134);
private Font italic_font = new Font("Microsoft YaHei UI", 15F, FontStyle.Italic, GraphicsUnit.Point, 134); private Font italic_font = new Font("Microsoft YaHei UI", 15F, FontStyle.Italic, GraphicsUnit.Point, 134);
private MD5 MD5 = MD5.Create();
private SHA1 SHA1 = SHA1.Create();
private void ID_Enter(object sender, EventArgs e) private void ID_Enter(object sender, EventArgs e)
{ {
if (ID.Font != normal_font) if (ID.Font != normal_font)
@ -64,15 +76,110 @@ namespace CSS_Solution.Forms
private void SavePassword_CheckedChanged(object sender, EventArgs e) private void SavePassword_CheckedChanged(object sender, EventArgs e)
{ {
if (sender == null||save_disk_info_showed) if (sender == null || save_disk_info_showed)
return; return;
CheckBox? checkBox = sender as CheckBox; CheckBox? checkBox = sender as CheckBox;
if (checkBox == null) return; if (checkBox == null) return;
if(checkBox.Checked) if (checkBox.Checked)
{ {
save_disk_info_showed = true; save_disk_info_showed = true;
MessageBox.Show("注意,密码确实会保存为密文,但是该密文可直接用于登录网站(教务管理系统),理想状态下,该密码应当仅保存在内存中。"); MessageBox.Show("注意,密码确实会保存为密文,但是该密文可直接用于登录网站(教务管理系统),理想状态下,该密码应当仅保存在内存中。");
} }
} }
private void record_Click(object sender, EventArgs e)
{
try
{
var (username, password) = Code_Preprocess();
Code_Save(username, password);
if (MessageBox.Show("是否进行一次登录测试,以确保账号密码正确?", "测试输入?", MessageBoxButtons.YesNo) == DialogResult.Yes)
Task.Run(() => Code_TestAsync());
else
Show_info("信息已记录");
}
catch (Exception ex)
{
Show_info(ex.Message);
}
}
private void test_Click(object sender, EventArgs e)
{
try
{
var (username, password) = Code_Preprocess();
if(username!=Settings.etc.Username||password!=Settings.etc.Password)
Code_Save(username ,password);
Task.Run(()=>Code_TestAsync());
}
catch (Exception ex)
{
Show_info(ex.Message);
}
}
private void Code_Save(string username, string password)
{
if (SavePassword.CheckState == CheckState.Checked)
Settings.etc.Write_Code_To_File(username, password);
Settings.etc.Username = username;
Settings.etc.Password = password;
}
private async Task Code_TestAsync()
{
Invoke(() => info.Text = "测试中,可能需要一点时间");
bool status = false;
string error_info = "";
await Task.Run(() =>
{
bool not_done = true;
try
{
Task_handler.index_Task_Handler.Make_request((result) =>
{
Task_handler.login_Task_Handler.Make_request((fresult) =>
{
not_done = false;
status = !fresult.isError();
error_info = fresult.Error_info;
}, result);
});
}
catch (Exception ex)
{
status = false;
error_info = ex.Message;
}
while (not_done) ;
});
Invoke(() => { if (status) Show_info("测试成功"); else Show_info("测试失败" + error_info); });
}
private (string,string) Code_Preprocess()
{
string username = ID.Text, password = this.password.Text;
if (username == null || username.Length == 0)
throw new InvalidOperationException("账号不能为空");
if (password == null || password.Length == 0)
throw new InvalidOperationException("怕密码泄露可以不用");
password = username + password;
username = Convert.ToHexString(MD5.ComputeHash(Encoding.UTF8.GetBytes(username))).ToLower();
password = Convert.ToHexString(SHA1.ComputeHash(Encoding.UTF8.GetBytes(password))).ToLower();
if (!Settings.etc.Agree_use)
if (MessageBox.Show("你需要为自己造成的后果负责,同一个账号的大量请求可不是什么正常的事,作者不会也无法对任何后果负责", "Are you sure about that?", MessageBoxButtons.YesNo) == DialogResult.No)
throw new Exception("Wise choice");
else
Settings.etc.Do_Agree_use();
return (username, password);
}
private void errorInfo_CountDown_Tick(object sender, EventArgs e)
{
errorInfo_CountDown.Enabled = false;
info.Visible = false;
}
private void Show_info(string s)
{
info.Text = s;
info.Visible = true;
errorInfo_CountDown.Enabled = true;
}
} }
} }

View File

@ -117,4 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="errorInfo_CountDown.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@ -16,7 +16,7 @@ namespace CSS_Solution
to_course, to_course,
change_class change_class
} }
public class Initialize class Initialize
{ {
#if DEBUG #if DEBUG
public static readonly string settings_File_Path = "AppSettings_Debug.json"; public static readonly string settings_File_Path = "AppSettings_Debug.json";
@ -29,12 +29,12 @@ namespace CSS_Solution
Console.WriteLine(builder); Console.WriteLine(builder);
builder.AddConsole(); builder.AddConsole();
}); });
public static Task_handler task_Handler = new Task_handler();
public static ILogger default_logger = loggerFactory.CreateLogger("Default logger"); public static ILogger default_logger = loggerFactory.CreateLogger("Default logger");
public static HttpClientPool hc_pool = new HttpClientPool(); public static HttpClientPool hc_pool = new HttpClientPool();
public Initialize() public Initialize()
{ {
Program.exceptionHandler = new ExceptionHandler(); Program.exceptionHandler = new ExceptionHandler();
} }
} }
internal static class Program internal static class Program

View File

@ -32,7 +32,7 @@ namespace CSS_Solution.Request
{ {
try try
{ {
HttpClient client = Initialize.hc_pool.getHttpClient(request_type); HttpClient client = Initialize.hc_pool.GetHttpClient(request_type);
HttpRequestMessage request_Message = new HttpRequestMessage(HttpMethod.Get, url); HttpRequestMessage request_Message = new HttpRequestMessage(HttpMethod.Get, url);
List<(string, string)> request_header = Settings.get_Index_Header.Get_header(); List<(string, string)> request_header = Settings.get_Index_Header.Get_header();
foreach (var (key, value) in request_header) foreach (var (key, value) in request_header)

View File

@ -10,15 +10,23 @@ namespace CSS_Solution.Request
public class HttpClientPool public class HttpClientPool
{ {
HttpClient[] httpClients = new HttpClient[5]; HttpClient[] httpClients = new HttpClient[5];
HttpClientHandler[] httpClientHandlers = new HttpClientHandler[5];
public HttpClientPool() public HttpClientPool()
{ {
for (int i = 0; i < httpClients.Length; i++) for (int i = 0; i < httpClients.Length; i++)
if(i==(int)Request_Type.to_course) if (i == (int)Request_Type.to_course)
httpClients[i] = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false }); {
else httpClientHandlers[i] = new HttpClientHandler() { AllowAutoRedirect = false };
httpClients[i] = new HttpClient(); httpClients[i] = new HttpClient(httpClientHandlers[i]);
} }
public HttpClient getHttpClient(Request_Type request_Type) => httpClients[(int)request_Type]; else
{
httpClientHandlers[i] = new HttpClientHandler();
httpClients[i] = new HttpClient(httpClientHandlers[i]);
}
}
public HttpClient GetHttpClient(Request_Type request_Type) => httpClients[(int)request_Type];
public HttpClientHandler GetHttpClientHandler(Request_Type request_Type) => httpClientHandlers[(int)request_Type];
} }

View File

@ -40,7 +40,7 @@ namespace CSS_Solution.Request
try try
{ {
{ {
HttpClient client = Initialize.hc_pool.getHttpClient(Request_Type.get_code); HttpClient client = Initialize.hc_pool.GetHttpClient(Request_Type.get_code);
HttpRequestMessage code_request_Message = new HttpRequestMessage(HttpMethod.Post, getCode_url); HttpRequestMessage code_request_Message = new HttpRequestMessage(HttpMethod.Post, getCode_url);
code_request_Message.Headers.Add("Cookie", JSESSIONID); code_request_Message.Headers.Add("Cookie", JSESSIONID);
foreach (var (key, value) in Settings.getCode_Header.Get_Request_header()) foreach (var (key, value) in Settings.getCode_Header.Get_Request_header())
@ -53,18 +53,17 @@ namespace CSS_Solution.Request
code = await code_message.Content.ReadAsStringAsync(); code = await code_message.Content.ReadAsStringAsync();
} }
{ {
HttpClient client = Initialize.hc_pool.getHttpClient(request_type); HttpClient client = Initialize.hc_pool.GetHttpClient(request_type);
HttpRequestMessage login_request_Message = new HttpRequestMessage(HttpMethod.Post, login_url); HttpRequestMessage login_request_Message = new HttpRequestMessage(HttpMethod.Post, login_url);
login_request_Message.Headers.Add("Cookie", JSESSIONID); login_request_Message.Headers.Add("Cookie", JSESSIONID);
foreach (var (key, value) in Settings.login_Header.Get_Request_header()) foreach (var (key, value) in Settings.login_Header.Get_Request_header())
login_request_Message.Headers.Add(key, value); login_request_Message.Headers.Add(key, value);
List<KeyValuePair<string, string>> response_body = Settings.login_Body.Get_Request_body(last_result.RND, code); List<KeyValuePair<string, string>> request_body = Settings.login_Body.Get_Request_body(last_result.RND, code);
login_request_Message.Content = new FormUrlEncodedContent(response_body); login_request_Message.Content = new FormUrlEncodedContent(request_body);
login_request_Message.Content.Headers.ContentType = new MediaTypeHeaderValue(Settings.login_Header.Content_Type); login_request_Message.Content.Headers.ContentType = new MediaTypeHeaderValue(Settings.login_Header.Content_Type);
HttpResponseMessage message = await client.SendAsync(login_request_Message); HttpResponseMessage message = await client.SendAsync(login_request_Message);
message.EnsureSuccessStatusCode(); message.EnsureSuccessStatusCode();
string ans = await message.Content.ReadAsStringAsync(); string ans = await message.Content.ReadAsStringAsync();
foreach (var header in message.Headers) foreach (var header in message.Headers)
{ {
if (header.Key != "Set-Cookie") if (header.Key != "Set-Cookie")
@ -87,10 +86,7 @@ namespace CSS_Solution.Request
success(id); success(id);
}); });
} }
public List<KeyValuePair<string, string>>? Get_cookie() public List<KeyValuePair<string, string>>? Get_cookie() => respond_cookie;
{
return respond_cookie;
}
public Task Run() public Task Run()
{ {
task.Start(); task.Start();

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -34,6 +35,13 @@ namespace CSS_Solution.Request
public Get_Index_Result(Get_Index get_Index) public Get_Index_Result(Get_Index get_Index)
{ {
id = get_Index.Get_Id(); id = get_Index.Get_Id();
if (get_Index.Get_cookie() == null || get_Index.Get_cookie()?.Count() == 0)
{
foreach (Cookie cookie in Initialize.hc_pool.GetHttpClientHandler(Request_Type.get_index).CookieContainer.GetCookies(new Uri("http://sso.jwc.whut.edu.cn/Certification/login.do")))
if (cookie.Name == "JSESSIONID")
JSESSIONID = cookie.Value;
}
else
JSESSIONID = get_Index.Get_cookie()?.Select(element => new { element.Key, element.Value }).Where(element => element.Key.Equals("JSESSIONID")).First().Value; JSESSIONID = get_Index.Get_cookie()?.Select(element => new { element.Key, element.Value }).Where(element => element.Key.Equals("JSESSIONID")).First().Value;
RND = get_Index.Get_rnd(); RND = get_Index.Get_rnd();
dateTime = DateTime.Now; dateTime = DateTime.Now;
@ -84,8 +92,16 @@ namespace CSS_Solution.Request
i--; i--;
} }
JSESSIONID = login.Get_JSESSIONID(); JSESSIONID = login.Get_JSESSIONID();
try
{
CERLOGIN = cookie?.Select(x => x).Where(element => element.Key.Equals("CERLOGIN")).First().Value; CERLOGIN = cookie?.Select(x => x).Where(element => element.Key.Equals("CERLOGIN")).First().Value;
cookie?.Add(new KeyValuePair<string, string>("JSESSIONID", JSESSIONID)); cookie?.Add(new KeyValuePair<string, string>("JSESSIONID", JSESSIONID));
}
catch (InvalidOperationException)
{
if (login.Get_cookie() == null || login.Get_cookie()?.Count == 0)
error_info = "Login_failed, Consider account password error!";
}
dateTime = DateTime.Now; dateTime = DateTime.Now;
error_info = ""; error_info = "";
} }

View File

@ -12,6 +12,17 @@ namespace CSS_Solution.Request
public static Index_Task_handler index_Task_Handler = new Index_Task_handler(); public static Index_Task_handler index_Task_Handler = new Index_Task_handler();
public static Login_Task_handler login_Task_Handler = new Login_Task_handler(); public static Login_Task_handler login_Task_Handler = new Login_Task_handler();
public static ToCourse_Task_Handler toCourse_Task_Handler = new ToCourse_Task_Handler(); public static ToCourse_Task_Handler toCourse_Task_Handler = new ToCourse_Task_Handler();
public bool Code_Test()
{
try
{
return true;
}
catch(Exception)
{
return false;
}
}
} }
class Index_Task_handler class Index_Task_handler
{ {
@ -102,6 +113,7 @@ namespace CSS_Solution.Request
return; return;
} }
tasks.Remove(id); tasks.Remove(id);
Initialize.default_logger.LogInformation($"[{DateTime.Now}] login success");
result_handler(result); result_handler(result);
} }
public void Login_failed(int id, Exception e, Login_Result? result = null) public void Login_failed(int id, Exception e, Login_Result? result = null)
@ -113,8 +125,11 @@ namespace CSS_Solution.Request
} }
if (result == null) if (result == null)
result = new Login_Result(e.Message); result = new Login_Result(e.Message);
else if (result.Error_info.Length == 0)
result.Error_info = "Login:" + e.Message;
else else
result.Error_info = e.Message; result.Error_info = "Login:" + e.Message + "error_info:" + result.Error_info;
Initialize.default_logger.LogWarning($"[{DateTime.Now}] {result.Error_info}");
tasks[id].Item2(result); tasks[id].Item2(result);
tasks.Remove(id); tasks.Remove(id);
} }

View File

@ -41,7 +41,7 @@ namespace CSS_Solution.Request
request_Message.Headers.Add(key, value); request_Message.Headers.Add(key, value);
return request_Message; return request_Message;
}; };
HttpClient client = Initialize.hc_pool.getHttpClient(request_type); HttpClient client = Initialize.hc_pool.GetHttpClient(request_type);
HttpResponseMessage message; HttpResponseMessage message;
do do
{ {

View File

@ -166,7 +166,6 @@ namespace CSS_Solution
} }
public class Login_body public class Login_body
{ {
List<KeyValuePair<string, string>> body = new List<KeyValuePair<string, string>>();
public Login_body() public Login_body()
{ {
MsgID = ""; MsgID = "";
@ -187,26 +186,32 @@ namespace CSS_Solution
} }
public List<KeyValuePair<string, string>> Get_Request_body(string rnd, string code) public List<KeyValuePair<string, string>> Get_Request_body(string rnd, string code)
{ {
if (body.Count != 0) List<KeyValuePair<string, string>> body = new List<KeyValuePair<string, string>>();
return body;
Type type = typeof(Login_body); Type type = typeof(Login_body);
PropertyInfo[] propertyInfo = type.GetProperties(); PropertyInfo[] propertyInfo = type.GetProperties();
foreach (PropertyInfo property in propertyInfo) foreach (PropertyInfo property in propertyInfo)
{ {
var val = (property.Name, property.GetValue(this) as string); var val = (property.Name, property.GetValue(this) as string);
if (val.Name == "rnd")
{
body.Add(new KeyValuePair<string, string>(val.Name, rnd));
continue;
}
if (val.Name == "code")
{
body.Add(new KeyValuePair<string, string>(val.Name, code));
continue;
}
if (val.Item2 == null) if (val.Item2 == null)
throw new ConfigurationErrorsException("Default header get error"); throw new ConfigurationErrorsException("Default header get error");
switch(val.Name)
{
case "rnd":
body.Add(new KeyValuePair<string, string>(val.Name, rnd));
break;
case "code":
body.Add(new KeyValuePair<string, string>(val.Name, code));
break;
case "userName1":
body.Add(new KeyValuePair<string, string>(val.Name, Settings.etc.Username?.Length == 0 ? val.Item2 : Settings.etc.Username ?? val.Item2));
break;
case "password1":
body.Add(new KeyValuePair<string, string>(val.Name, Settings.etc.Password?.Length == 0 ? val.Item2 : Settings.etc.Password ?? val.Item2));
break;
default:
body.Add(new KeyValuePair<string, string>(val.Name.Last() == '_' ? val.Name.Remove(val.Name.Length - 1) : val.Name, val.Item2)); body.Add(new KeyValuePair<string, string>(val.Name.Last() == '_' ? val.Name.Remove(val.Name.Length - 1) : val.Name, val.Item2));
break;
}
} }
return body; return body;
} }
@ -285,13 +290,14 @@ namespace CSS_Solution
this.keyinfo = keyinfo; this.keyinfo = keyinfo;
_ = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond).ToString(); _ = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond).ToString();
} }
} }
public class Etc public class Etc
{ {
public Etc() public Etc()
{ {
StartTime_Str = ""; StartTime_Str = "";
Username = "";
Password = "";
Initialize.configuration.Bind("Etc", this); Initialize.configuration.Bind("Etc", this);
if (StartTime_Str.Length == 0) if (StartTime_Str.Length == 0)
StartTime = DateTime.Now; StartTime = DateTime.Now;
@ -310,35 +316,62 @@ namespace CSS_Solution
} }
public string StartTime_Str { get; set; } public string StartTime_Str { get; set; }
public DateTime StartTime { get; set; } public DateTime StartTime { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool Agree_use { get; set; }
public void Set_StartTime(int year, int month, int day, int hour, int minute, int second) public void Set_StartTime(int year, int month, int day, int hour, int minute, int second)
{ {
StartTime = new DateTime(year, month, day, hour, minute, second); StartTime = new DateTime(year, month, day, hour, minute, second);
StartTime_Str = year.ToString() + "-" + month.ToString() + "-" + day.ToString() + "-" + hour.ToString() + "-" + minute.ToString() + "-" + second.ToString(); StartTime_Str = year.ToString() + "-" + month.ToString() + "-" + day.ToString() + "-" + hour.ToString() + "-" + minute.ToString() + "-" + second.ToString();
} }
public void Write_StartTime_To_File() private JObject Json_Read()
{ {
JObject jsonObject;
string filePath = Initialize.settings_File_Path; string filePath = Initialize.settings_File_Path;
using (StreamReader fileReader = File.OpenText(filePath)) using (StreamReader fileReader = File.OpenText(filePath))
{ {
using (JsonTextReader reader = new JsonTextReader(fileReader)) using (JsonTextReader reader = new JsonTextReader(fileReader))
{ {
JObject jsonObject = (JObject)JToken.ReadFrom(reader); jsonObject = (JObject)JToken.ReadFrom(reader);
}
}
return jsonObject;
}
private void Json_Write(JObject jsonObject)
{
string filePath = Initialize.settings_File_Path;
using (StreamWriter fileWriter = new StreamWriter(filePath))
{
fileWriter.Write(jsonObject.ToString());
}
}
public void Write_StartTime_To_File()
{
JObject jsonObject = Json_Read();
#pragma warning disable CS8602 // 解引用可能出现空引用。 #pragma warning disable CS8602 // 解引用可能出现空引用。
jsonObject["Etc"]["StartTime_Str"] = "StartTime_Str"; jsonObject["Etc"]["StartTime_Str"] = StartTime_Str;
#pragma warning restore CS8602 // 解引用可能出现空引用。 #pragma warning restore CS8602 // 解引用可能出现空引用。
fileReader.BaseStream.Seek(0, SeekOrigin.Begin); Json_Write(jsonObject);
fileReader.DiscardBufferedData(); }
using (StreamWriter fileWriter = File.CreateText(filePath)) //userName md5 password1 username+password sha1
public void Write_Code_To_File(string username, string password)
{ {
using (JsonTextWriter writer = new JsonTextWriter(fileWriter)) JObject jsonObject = Json_Read();
#pragma warning disable CS8602 // 解引用可能出现空引用。
jsonObject["login_body"]["userName1"] = username;
jsonObject["login_body"]["password1"] = password;
#pragma warning restore CS8602 // 解引用可能出现空引用。
Json_Write(jsonObject);
}
internal void Do_Agree_use()
{ {
writer.Formatting = Formatting.Indented; JObject jsonObject = Json_Read();
jsonObject.WriteTo(writer); Agree_use = true;
writer.Flush(); #pragma warning disable CS8602 // 解引用可能出现空引用。
} jsonObject["Etc"]["Agree_use"] = true;
} #pragma warning restore CS8602 // 解引用可能出现空引用。
} Json_Write(jsonObject);
}
} }
} }