I'll show you how to do this in Windows Forms, but you should understand that doing things this way:
1. is unusual: who knows how Windows Forms may change in the future in a way that would make this technique cause an error. in fact, I've deliberately used every rather unusual technique I know in this method (for the fun of it) :)
2. as long as the username and password are in "plain text" anywhere, in any program, anyone with some technical skill, and the motivation, is going to be able to hack and get access to the plain text. so: don't think this technique is any kind of strong "protection: there is no substitute for using encryption, and Windows Security services, and your databases security facilities !
How to do this:
0. create your Application in the usual way: we will modify the 'Program file used to start the Application in later steps. I'll assume your Main Form is named 'MainForm.
1a. Create a LogIn Form, 'LogInForm, with two TextBoxes: tbUserName, tbPassword
set 'tbPassword's UseSystemPasswordChar property to 'true, or set its 'PasswordChar property to a character you choose.
1b. do the right thing to set the appeaerance/behavior properties of the the log-in Form and its TextBoxes so they reflect your design. for example, I'd choose to set the 'ShowInTaskBar property of the log-in Form to false.
2. Open the Program.cs file of your WinForm solution, and modify like this:
using System;
using System.Windows.Forms;
namespace YourNameSpace
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
lgIn = new Login();
lgIn.ShowDialog();
if (go)
{
Application.Run(new MainForm());
}
}
public static Login lgIn;
public static bool go = false, c1 = false, c2 = false;
public static void TX(object sender, EventArgs eventArgs)
{
var t = sender as TextBox;
switch (t.Name)
{
case "tbUserName":
c1 = t.Text == "username";
break;
case "tbPassword":
c2 = t.Text == "password";
break;
}
if (c1 && c2)
{
go = true;
lgIn.Dispose();
}
}
}
}
3. Code the LogIn form:
using System;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
tbPassword.TextChanged += (obj, eargs) => Program.TX(tbPassword, eargs);
tbUserName.TextChanged += (obj, eargs) => Program.TX(tbUserName, eargs);
this.FormClosed += (obj, eargs) =>
{
MessageBox.Show((Program.go)
? "You have logged in."
: "You have not logged in.");
};
}
}
}
How does this "work:"
1. code in the Program.cs static Main method is executed:
a. a new LogIn Form is created and displayed using 'ShowDialog which pauses the main thread ... stops the show.
b. in the Program Class a method that can be used as an EventHandler for TextBox TextChanged is defined: 'TX.
2. when the Login Form is created:
a. event handlers are assigned for the 'TextChanged Event of both TextBoxes: both of these invoke the 'TX event handler defined in Program.cs, which is static.
b. an event handler is assigned for the LogIn form's 'Closed Event. in that event code, the Program.cs static boolean variable 'go is used to present a message to the user telling them whether or not they are logged-in.
3. as the user types in the two TextBoxes on the LogIn Form:
a. the 'TX event handler in Program.cs is called with each character entered: a test is done to see if the log-in is complete.
4. when the LogIn is successfully completed:
a. the Disposing of the LogIn form unblocks the main thread.
b. then the Application is started in the usual way: the MainForm is displayed.