Browse Source

chore: Using PasswordBox for password now

tags/0.1.0
OctaAcid 2 years ago
parent
commit
e106f82ed3
4 changed files with 108 additions and 41 deletions
  1. +56
    -0
      installer/Installer/Common.cs
  2. +1
    -1
      installer/Installer/MainWindow.xaml
  3. +39
    -32
      installer/Installer/Model.cs
  4. +12
    -8
      installer/Installer/ViewModel.cs

+ 56
- 0
installer/Installer/Common.cs View File

@@ -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
}
}

/// <summary>
/// Password 附加属性,来自https://blog.csdn.net/qq_43562262/article/details/121786337
/// </summary>
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;
}
}

}

+ 1
- 1
installer/Installer/MainWindow.xaml View File

@@ -71,7 +71,7 @@
<TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" Visibility="{Binding LoginVis}" />
<TextBlock Grid.Row="3" Grid.Column="0" Text="密码:" Visibility="{Binding LoginVis}" />
<TextBox Grid.Row="1" Grid.Column="1" Name="Username" Visibility="{Binding LoginVis}" Text="{Binding Username}" />
<TextBox Grid.Row="3" Grid.Column="1" Name="Password" Visibility="{Binding LoginVis}" Text="{Binding Password}" />
<PasswordBox Grid.Row="3" Grid.Column="1" Name="Password" Visibility="{Binding LoginVis}" c:PasswordHelper.Attach="True" c:PasswordHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<!--<CheckBox Grid.Row="5" Grid.Column="0" Visibility="{Binding LoginVis}">记住我</CheckBox>-->
<TextBlock Grid.Row="5" Grid.Column="1" Foreground="Red" Text=" 用户名或密码错误!" Visibility="{Binding LoginFailVis}"/>
</Grid>


+ 39
- 32
installer/Installer/Model.cs View File

@@ -124,7 +124,7 @@ namespace starter.viewmodel.settings
}
}

public async Task<bool> Login()
public async Task<int> 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<bool> LoginToEEsast(HttpClient client, string useremail, string password)
async public Task<int> 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<Dictionary<string, string>>(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<Dictionary<string, string>>(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;
}
}
/// <summary>


+ 12
- 8
installer/Installer/ViewModel.cs View File

@@ -481,15 +481,19 @@ namespace starter.viewmodel.settings
{
clickLoginCommand = new BaseCommand(new Action<object>(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");
}));


Loading…
Cancel
Save