English 中文(简体)
简单的WPF 认证
原标题:A simple WPF Authentication

How to create a simple WPF Authentication for WPF application? For example: First time a user should registry then login. Users login and password should be saved as txt file(encrypted). If process of authentication is successful,then it should redirect to another existed window.

I m a beginner in WPF. I ve searched about this question,but didn t find what I need. I need a simple,step by step explanation of how to do it.

提前感谢!

问题回答

我也正在学习,以便做一个非常简单的榜样。 这可能是不专业和不安全的,但认为(hope)可以推广:

首先,你们需要创建简单的世界森林基金窗口(使用txt/btn+Name命名公约):

windows

两个窗口加起来

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();
    }
}

登记册窗口代码:

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();
    }
}

为了工作申请,需要查阅“Users.txt”档案,这些档案必须放在同一个文件夹上。

Notes:

  1. It will be better if you will use some proper encryption functions and probably create separate class for it. Additionally i am almost sure that it will not work properly with login and password which contains the last 3 characters from the end of ASCII tables.
  2. In my opinion it is a bad idea to store login and password data in *.txt file :).
  3. As far i know C# code is very easily reverse engineered so probably it will be better to hide encryption/decryption part somehow. I do not know much about it, but u will be able to read more [here] 2 and probably uncle Google will be able to help.
  4. Code is very simple and there is probably a lot of possibilities to extend it (more file handling stuff, TextBox validation for proper input and password strength calculations)




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签