forked from HenJigg/my-todoapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into main
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MyToDo.Shared | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static string GetMD5(this string data) | ||
{ | ||
if (string.IsNullOrWhiteSpace(data)) | ||
throw new ArgumentNullException(nameof(data)); | ||
|
||
var hash = MD5.Create().ComputeHash(Encoding.Default.GetBytes(data)); | ||
return Convert.ToBase64String(hash);//将加密后的字节数组转换为加密字符串 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.Xaml.Behaviors; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
|
||
namespace MyToDo.Common | ||
{ | ||
public class PassWordExtensions | ||
{ | ||
public static readonly DependencyProperty PasswordProperty = | ||
DependencyProperty.RegisterAttached("Password", | ||
typeof(string), typeof(PassWordExtensions), | ||
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged)); | ||
|
||
private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | ||
{ | ||
PasswordBox passwordBox = sender as PasswordBox; | ||
string password = (string)e.NewValue; | ||
if (passwordBox != null && passwordBox.Password != password) | ||
passwordBox.Password = password; | ||
} | ||
|
||
public static string GetPassword(DependencyObject dp) | ||
{ | ||
return (string)dp.GetValue(PasswordProperty); | ||
} | ||
|
||
public static void SetPassword(DependencyObject dp, string value) | ||
{ | ||
dp.SetValue(PasswordProperty, value); | ||
} | ||
} | ||
|
||
public class PasswordBoxBehavior : Behavior<PasswordBox> | ||
{ | ||
protected override void OnAttached() | ||
{ | ||
base.OnAttached(); | ||
AssociatedObject.PasswordChanged += OnPasswordChanged; | ||
} | ||
|
||
private static void OnPasswordChanged(object sender, RoutedEventArgs e) | ||
{ | ||
PasswordBox passwordBox = sender as PasswordBox; | ||
string password = PassWordExtensions.GetPassword(passwordBox); | ||
if (passwordBox != null && passwordBox.Password != password) | ||
PassWordExtensions.SetPassword(passwordBox, passwordBox.Password); | ||
} | ||
|
||
protected override void OnDetaching() | ||
{ | ||
base.OnDetaching(); | ||
AssociatedObject.PasswordChanged -= OnPasswordChanged; | ||
} | ||
} | ||
} |