1

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.

Thanks in advance! :)

H.B.
  • 166,899
  • 29
  • 327
  • 400
user617275
  • 319
  • 1
  • 3
  • 6
  • possible duplicate of [Authentication and Roles in WPF](http://stackoverflow.com/questions/4732176/authentication-and-roles-in-wpf) – Dan J Jun 14 '11 at 21:39
  • So is user management scoped to the local installation of the application or is there a centralized authentication service? – Christoph Jun 14 '11 at 23:41
  • A good tutorial: http://blog.magnusmontin.net/2013/03/24/custom-authorization-in-wpf/ – PiotrWolkowski Nov 21 '15 at 04:09

1 Answers1

19

I am also learning so in order to exercise a bit i have created a very simple example for you. It is probably unprofessional and unsafe but i think (hope) it is possible to extend it somehow :).

Firstly you need to create simple WPF windows (use txt/btn+Name naming convention):

windows

For both windows add

using System.IO;

Then you need to add events for buttons and modify code for both windows:

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

And code for Register window:

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

In order to work application need to have access to file "Users.txt" which needs to be placed in the same folder.

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)
miken32
  • 42,008
  • 16
  • 111
  • 154
Archibald
  • 846
  • 10
  • 24
  • 5
    Someone that actually puts this much effort into answering a question which is more of a request of code then an actual question get's a vote up from me anyday! – F.B. ten Kate Sep 22 '11 at 13:32
  • Great response. You're right though, it's a terrible idea to store usernames / password combinations in TXT files. You would probably want to store them in some sort of database (I use SQL) and look at the database for that user. Also, in more advanced systems, you usually have Groups, Roles and Permissions which determine not only if the user exist and can login, but also if he's an administrator, or a regular user, thus determining the features / screens he or she are able to see. Anyway, great example, you got my upvote ;) – Jonathan Perry Apr 07 '13 at 07:10