Sic04/SicUI/Models/RecipeEditors/EditorPassChangeView.xaml.cs

140 lines
3.9 KiB
C#

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;
}
}
Models.RecipeEditors.RecipeEditorViewModel revm = null;
public EditorPassChangeView(Object obj)
{
InitializeComponent();
//
revm = obj as Models.RecipeEditors.RecipeEditorViewModel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var sCurPass = CurPass.Password;
var sNewPass1 = NewPass1.Password;
var sNewPass2 = NewPass2.Password;
//
if(sCurPass.Length == 0)
{
MessageBox.Show("Current Password can't be empty.", "Error", MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
if(sNewPass1.Length == 0)
{
MessageBox.Show("Please input the New Password.", "Error", MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
if(sNewPass2.Length == 0)
{
MessageBox.Show("Please input the Confirm Password.","Error", MessageBoxButton.OK,
MessageBoxImage.Error) ;
return;
}
if(sNewPass1 != sNewPass2)
{
MessageBox.Show("The new passwords are different.","Error", MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
if(!CheckPass(sCurPass))
{
MessageBox.Show("Current password is wrong.","Error", MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
//
if (WriteNewPass(sNewPass1))
{
MessageBox.Show("Password has been changed successfully.","Succeeded", MessageBoxButton.OK,
MessageBoxImage.Information);
Close();
}
else
{
MessageBox.Show("It is failed to save new password.","Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
private bool CheckPass(string sPassword)
{
if(Password == "0")
{
//初始值,未修改过密码
return true;
}
//
MD5 m = MD5.Create();
byte[] bPass = Encoding.UTF8.GetBytes(sPassword);
byte[] md5bPass = m.ComputeHash(bPass);
string sPass = "";
for(int i=0;i<md5bPass.Length;i++)
{
sPass += md5bPass[i].ToString("X2");
}
return sPass == Password;
}
private bool WriteNewPass(string sPassword)
{
//
try
{
MD5 m = MD5.Create();
byte[] bPass = Encoding.UTF8.GetBytes(sPassword);
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;
}
}
}