一种简单的WPF身份验证
如何为WPF应用程序创建简单的WPF身份验证? 例如: 用户第一次应该注册然后登录。 用户登录名和密码应保存为txt文件(加密)。 若身份验证过程成功,那个么它应该重定向到另一个已存在的窗口 我是WPF的初学者。 我已经搜索了这个问题,但没有找到我需要的。 我需要一个简单的,一步一步的解释如何做到这一点一种简单的WPF身份验证,wpf,authentication,Wpf,Authentication,如何为WPF应用程序创建简单的WPF身份验证? 例如: 用户第一次应该注册然后登录。 用户登录名和密码应保存为txt文件(加密)。 若身份验证过程成功,那个么它应该重定向到另一个已存在的窗口 我是WPF的初学者。 我已经搜索了这个问题,但没有找到我需要的。 我需要一个简单的,一步一步的解释如何做到这一点 提前感谢!:) 我也在学习,所以为了锻炼身体,我为大家创建了一个非常简单的例子。这可能是不专业和不安全的,但我认为(希望)有可能以某种方式扩展它:) 首先,您需要创建简单的WPF窗口(使用txt
提前感谢!:) 我也在学习,所以为了锻炼身体,我为大家创建了一个非常简单的例子。这可能是不专业和不安全的,但我认为(希望)有可能以某种方式扩展它:) 首先,您需要创建简单的WPF窗口(使用txt/btn+名称命名约定): 对于两个窗口,添加
using System.IO;
然后,您需要为按钮添加事件,并修改两个窗口的代码:
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
// This is really bad/weak encryption method
String WeakDecryptMethod(String textIn)
{
Char[] temp = textIn.ToArray<Char>();
for (int i = 0; i < textIn.Length; i++)
{
temp[i] = (char)((int)temp[i] - 3);
}
return new String(temp);
}
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
RegisterWindow newWindow = new RegisterWindow();
newWindow.ShowDialog();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
// If file exist and login and password are "correct"
if (File.Exists("Users.txt")
&& txtLogin.Text.Length >= 4
&& txtPass.Text.Length >= 4)
{
using (StreamReader streamReader = new StreamReader("Users.txt"))
{
// While there is something in streamReader read it
while (streamReader.Peek() >= 0)
{
String decryptedLogin = WeakDecryptMethod(streamReader.ReadLine());
String decryptedPass = WeakDecryptMethod(streamReader.ReadLine());
if (decryptedLogin == txtLogin.Text && decryptedPass == txtPass.Text)
{
ProtectedWindow protectedWindow = new ProtectedWindow();
this.Close();
protectedWindow.Show();
break;
}
}
}
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
公共部分类登录窗口:窗口
{
公共登录窗口()
{
初始化组件();
}
//这是一种非常糟糕/脆弱的加密方法
字符串WeakDecryptMethod(字符串文本输入)
{
Char[]temp=textIn.ToArray();
for(int i=0;i=4
&&txtPass.Text.Length>=4)
{
使用(StreamReader StreamReader=newstreamreader(“Users.txt”))
{
//当streamReader中有内容时,请阅读它
while(streamReader.Peek()>=0)
{
String decryptedLogin=WeakDecryptMethod(streamReader.ReadLine());
String decryptedPass=WeakDecryptMethod(streamReader.ReadLine());
if(decryptedLogin==txtLogin.Text&&decryptedPass==txtPass.Text)
{
ProtectedWindow ProtectedWindow=新的ProtectedWindow();
这个。关闭();
protectedWindow.Show();
打破
}
}
}
}
}
私有void btnCancel\u单击(对象发送者、路由目标)
{
这个。关闭();
}
}
注册窗口的名称和代码:
public partial class RegisterWindow : Window
{
public RegisterWindow()
{
InitializeComponent();
}
// This is really bad/weak method to encrypt files
String WeakEncryptMethod(String textIn)
{
Char[] temp = textIn.ToArray<Char>();
for (int i = 0; i < textIn.Length; i++)
{
temp[i] = (char)((int)temp[i] + 3);
}
return new String(temp);
}
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
// If file exist and login and password are "correct"
if (File.Exists("Users.txt")
&& txtLogin.Text.Length >= 4
&& txtPass.Text.Length >= 4
&& txtPass.Text == txtPassCheck.Text)
{
StringBuilder stringBuilder = new StringBuilder();
using (StreamReader streamReader = new StreamReader("Users.txt"))
{
stringBuilder.Append(streamReader.ReadToEnd());
}
using (StreamWriter streamWriter = new StreamWriter("Users.txt"))
{
streamWriter.Write(stringBuilder.ToString());
streamWriter.WriteLine(WeakEncryptMethod(txtLogin.Text));
streamWriter.WriteLine(WeakEncryptMethod(txtPass.Text));
}
this.Close();
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
公共部分类注册表窗口:窗口
{
公共注册窗口()
{
初始化组件();
}
//这是一种非常糟糕/脆弱的文件加密方法
字符串加密方法(字符串文本输入)
{
Char[]temp=textIn.ToArray();
for(int i=0;i=4
&&txtPass.Text.Length>=4
&&Text==txtPassCheck.Text)
{
StringBuilder StringBuilder=新的StringBuilder();
使用(StreamReader StreamReader=newstreamreader(“Users.txt”))
{
追加(streamReader.ReadToEnd());
}
使用(StreamWriter StreamWriter=newstreamwriter(“Users.txt”))
{
streamWriter.Write(stringBuilder.ToString());
streamWriter.WriteLine(弱加密方法(txtLogin.Text));
streamWriter.WriteLine(弱加密方法(txtPass.Text));
}
这个。关闭();
}
}
私有void btnCancel\u单击(对象发送者、路由目标)
{
这个。关闭();
}
}
为了工作,应用程序需要访问需要放在同一文件夹中的文件“Users.txt”
注:
我也在学习,所以为了锻炼身体,我为你们创建了一个非常简单的例子。这可能是不专业和不安全的,但我认为(希望)有可能以某种方式扩展它:) 首先,您需要创建简单的WPF窗口(使用txt/btn+名称命名约定): 对于两个窗口,添加
using System.IO;
然后,您需要为按钮添加事件,并修改两个窗口的代码:
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
// This is really bad/weak encryption method
String WeakDecryptMethod(String textIn)
{
Char[] temp = textIn.ToArray<Char>();
for (int i = 0; i < textIn.Length; i++)
{
temp[i] = (char)((int)temp[i] - 3);
}
return new String(temp);
}
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
RegisterWindow newWindow = new RegisterWindow();
newWindow.ShowDialog();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
// If file exist and login and password are "correct"
if (File.Exists("Users.txt")
&& txtLogin.Text.Length >= 4
&& txtPass.Text.Length >= 4)
{
using (StreamReader streamReader = new StreamReader("Users.txt"))
{
// While there is something in streamReader read it
while (streamReader.Peek() >= 0)
{
String decryptedLogin = WeakDecryptMethod(streamReader.ReadLine());
String decryptedPass = WeakDecryptMethod(streamReader.ReadLine());
if (decryptedLogin == txtLogin.Text && decryptedPass == txtPass.Text)
{
ProtectedWindow protectedWindow = new ProtectedWindow();
this.Close();
protectedWindow.Show();
break;
}
}
}
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
公共部分类登录窗口:窗口
{
公共登录窗口()
{
初始化组件();
}
//这是一种非常糟糕/脆弱的加密方法
字符串WeakDecryptMethod(字符串文本输入)
{
Char[]temp=textIn.ToArray();
for(int i=0;i