From e106f82ed3e0123c02c96d18d5042ac383b64ada Mon Sep 17 00:00:00 2001 From: OctaAcid Date: Sun, 9 Apr 2023 01:17:56 +0800 Subject: [PATCH] chore: Using PasswordBox for password now --- installer/Installer/Common.cs | 56 +++++++++++++++++++++++ installer/Installer/MainWindow.xaml | 2 +- installer/Installer/Model.cs | 71 ++++++++++++++++------------- installer/Installer/ViewModel.cs | 20 ++++---- 4 files changed, 108 insertions(+), 41 deletions(-) diff --git a/installer/Installer/Common.cs b/installer/Installer/Common.cs index fc3a554..0f3697d 100644 --- a/installer/Installer/Common.cs +++ b/installer/Installer/Common.cs @@ -3,6 +3,8 @@ using System; using System.Windows.Input; using System.Globalization; using System.Windows.Data; +using System.Windows.Controls; +using System.Windows; namespace starter.viewmodel.common { @@ -98,4 +100,58 @@ namespace starter.viewmodel.common } } + /// + /// Password 附加属性,来自https://blog.csdn.net/qq_43562262/article/details/121786337 + /// + public class PasswordHelper + { + public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper), + new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged))); + + public static string GetPassword(DependencyObject d) + { + return (string)d.GetValue(PasswordProperty); + } + public static void SetPassword(DependencyObject d, string value) + { + d.SetValue(PasswordProperty, value); + } + + public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordHelper), + new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged))); + + public static string GetAttach(DependencyObject d) + { + return (string)d.GetValue(AttachProperty); + } + public static void SetAttach(DependencyObject d, string value) + { + d.SetValue(AttachProperty, value); + } + + static bool _isUpdating = false; + private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + PasswordBox pb = d as PasswordBox; + pb.PasswordChanged -= Pb_PasswordChanged; + if (!_isUpdating) + (d as PasswordBox).Password = e.NewValue.ToString(); + pb.PasswordChanged += Pb_PasswordChanged; + } + + private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + PasswordBox pb = d as PasswordBox; + pb.PasswordChanged += Pb_PasswordChanged; + } + + private static void Pb_PasswordChanged(object sender, RoutedEventArgs e) + { + PasswordBox pb = sender as PasswordBox; + _isUpdating = true; + SetPassword(pb, pb.Password); + _isUpdating = false; + } + } + } diff --git a/installer/Installer/MainWindow.xaml b/installer/Installer/MainWindow.xaml index 993efaa..48625cc 100644 --- a/installer/Installer/MainWindow.xaml +++ b/installer/Installer/MainWindow.xaml @@ -71,7 +71,7 @@ - + diff --git a/installer/Installer/Model.cs b/installer/Installer/Model.cs index 371bd97..a6ee504 100644 --- a/installer/Installer/Model.cs +++ b/installer/Installer/Model.cs @@ -124,7 +124,7 @@ namespace starter.viewmodel.settings } } - public async Task Login() + public async Task Login() { return await web.LoginToEEsast(client, Username, Password); } @@ -1057,43 +1057,50 @@ namespace WebConnect { public enum language { cpp, py }; public static string logintoken = ""; - async public Task LoginToEEsast(HttpClient client, string useremail, string password) + async public Task LoginToEEsast(HttpClient client, string useremail, string password) { string token = ""; - using (var response = await client.PostAsync("https://api.eesast.com/users/login", JsonContent.Create(new - { - email = useremail, - password = password, - }))) + try { - switch (response.StatusCode) + using (var response = await client.PostAsync("https://api.eesast.com/users/login", JsonContent.Create(new { - case System.Net.HttpStatusCode.OK: - Console.WriteLine("Success login"); - token = (System.Text.Json.JsonSerializer.Deserialize(await response.Content.ReadAsStreamAsync(), typeof(LoginResponse), new JsonSerializerOptions() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - }) as LoginResponse) - ?.Token ?? - throw new Exception("no token!"); - logintoken = token; - SaveToken(); - var info = JsonConvert.DeserializeObject>(await response.Content.ReadAsStringAsync()); - Downloader.UserInfo._id = info["_id"]; - Downloader.UserInfo.email = info["email"]; - break; + email = useremail, + password = password, + }))) + { + switch (response.StatusCode) + { + case System.Net.HttpStatusCode.OK: + Console.WriteLine("Success login"); + token = (System.Text.Json.JsonSerializer.Deserialize(await response.Content.ReadAsStreamAsync(), typeof(LoginResponse), new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }) as LoginResponse) + ?.Token ?? + throw new Exception("no token!"); + logintoken = token; + SaveToken(); + var info = JsonConvert.DeserializeObject>(await response.Content.ReadAsStringAsync()); + Downloader.UserInfo._id = info["_id"]; + Downloader.UserInfo.email = info["email"]; + break; - default: - int code = ((int)response.StatusCode); - Console.WriteLine(code); - if (code == 401) - { - //Console.WriteLine("邮箱或密码错误!"); - return false; - } - break; + default: + int code = ((int)response.StatusCode); + Console.WriteLine(code); + if (code == 401) + { + //Console.WriteLine("邮箱或密码错误!"); + return -1; + } + break; + } + return 0; } - return true; + } + catch + { + return -2; } } /// diff --git a/installer/Installer/ViewModel.cs b/installer/Installer/ViewModel.cs index b776fa8..8461acd 100644 --- a/installer/Installer/ViewModel.cs +++ b/installer/Installer/ViewModel.cs @@ -481,15 +481,19 @@ namespace starter.viewmodel.settings { clickLoginCommand = new BaseCommand(new Action(async o => { - if (!(await obj.Login())) + switch(await obj.Login()) { - obj.LoginFailed = true; - } - else - { - obj.LoginFailed = false; - Status = SettingsModel.Status.web; - this.RaisePropertyChanged("CoverVis"); + case -1: + obj.LoginFailed = true; + break; + case 0: + obj.LoginFailed = false; + Status = SettingsModel.Status.web; + this.RaisePropertyChanged("CoverVis"); + break; + case -2: + MessageBox.Show("无法连接服务器,请检查网络情况", "网络错误", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); + break; } this.RaisePropertyChanged("LoginFailVis"); }));