2022-09-19 09:16:33 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
|
|
|
namespace SicUI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// WinDataView.xaml 的交互逻辑
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class EditorPassChangeView : Window
|
|
|
|
|
{
|
|
|
|
|
private string _sPassword = "";
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
public string Password { get
|
|
|
|
|
{
|
|
|
|
|
return _sPassword; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_sPassword = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 10:49:29 +08:00
|
|
|
|
Models.RecipeEditors.RecipeEditorViewModel revm = null;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
|
|
|
|
|
public EditorPassChangeView(Object obj)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
//
|
2022-09-26 10:49:29 +08:00
|
|
|
|
revm = obj as Models.RecipeEditors.RecipeEditorViewModel;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
2022-09-26 10:49:29 +08:00
|
|
|
|
var sCurPass = CurPass.Password;
|
|
|
|
|
var sNewPass1 = NewPass1.Password;
|
|
|
|
|
var sNewPass2 = NewPass2.Password;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
//
|
|
|
|
|
if(sCurPass.Length == 0)
|
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("Current Password can't be empty.", "Error", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(sNewPass1.Length == 0)
|
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("Please input the New Password.", "Error", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(sNewPass2.Length == 0)
|
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("Please input the Confirm Password.","Error", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error) ;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-26 10:49:29 +08:00
|
|
|
|
if(sNewPass1 != sNewPass2)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("The new passwords are different.","Error", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-26 10:49:29 +08:00
|
|
|
|
if(!CheckPass(sCurPass))
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("Current password is wrong.","Error", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
if (WriteNewPass(sNewPass1))
|
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("Password has been changed successfully.","Succeeded", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Information);
|
|
|
|
|
Close();
|
2022-09-19 09:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
MessageBox.Show("It is failed to save new password.","Error", MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckPass(string sPassword)
|
|
|
|
|
{
|
2022-09-26 10:49:29 +08:00
|
|
|
|
if(Password == "0")
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
|
|
|
|
//初始值,未修改过密码
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
MD5 m = MD5.Create();
|
2022-09-26 10:49:29 +08:00
|
|
|
|
byte[] bPass = Encoding.UTF8.GetBytes(sPassword);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
byte[] md5bPass = m.ComputeHash(bPass);
|
|
|
|
|
string sPass = "";
|
|
|
|
|
for(int i=0;i<md5bPass.Length;i++)
|
|
|
|
|
{
|
|
|
|
|
sPass += md5bPass[i].ToString("X2");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 10:49:29 +08:00
|
|
|
|
return sPass == Password;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
private bool WriteNewPass(string sPassword)
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MD5 m = MD5.Create();
|
2022-09-26 10:49:29 +08:00
|
|
|
|
byte[] bPass = Encoding.UTF8.GetBytes(sPassword);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
byte[] md5bPass = m.ComputeHash(bPass);
|
|
|
|
|
string sPass = "";
|
|
|
|
|
for (int i = 0; i < md5bPass.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
sPass += md5bPass[i].ToString("X2");
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
if (revm != null)
|
|
|
|
|
{
|
|
|
|
|
return revm.ChanagePasswordReal(sPass);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|