How to set a password in Winform application

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4047 Views - Last Post: 21 November 2009 - 08:49 AM Rate Topic: -----

#1 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

How to set a password in Winform application

Posted 31 December 2008 - 01:52 PM

Can anyone tell me how to set a password in the C# application (Windows application) , like whenever I run the program it ask me for the password???
Is This A Good Question/Topic? 0
  • +

Replies To: How to set a password in Winform application

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How to set a password in Winform application

Posted 31 December 2008 - 02:08 PM

You can just add a TextBox to the startup form and ask the user to enter the password. Then, for a Click event handler of a Button control you can add a piece of verification code, to check whether the text entered in the TextBox control is the correct password or not (just compare strings). You can either set the password directly in the code or use application settings.
Was This Post Helpful? 0
  • +
  • -

#3 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

Re: How to set a password in Winform application

Posted 31 December 2008 - 02:12 PM

can i display a textbox to the user like a MessageBox?? like when a user click CTRL+F5 den the textbox appears first asking for the passwd , if the psswd is correct then only the application will run.. Or there any VS Studio.net support for suchthing?
Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How to set a password in Winform application

Posted 31 December 2008 - 02:40 PM

You can create a Windows Form like I described in my previous post. I don't know why do you want to use Ctrl+F5 to launch your application. Note, that the user won't use F5 or Ctrl+F5 to launch the application (like it is in Visual Studio). They will use the executable file.
Was This Post Helpful? 0
  • +
  • -

#5 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

Re: How to set a password in Winform application

Posted 31 December 2008 - 11:12 PM

ya xactly , thats wat i mean ! Whether a user runs a .exe file or build the application the MessageBox textfield occurs and asks for a password , if user enters the psswd correctly then only he is allowed to use the application otherwise not ... How will I do it?
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How to set a password in Winform application

Posted 31 December 2008 - 11:21 PM

Just create a Windows Form similar to the one on the picture:

Posted Image

Then, for the OK button add the verification code (basically, you compare the strings):

if (textBox1.Text == "fDHvnE34")
{
// Actions if password is correct
}
else
{
// Actions if password is incorrect
}



Also, set the PasswordChar property to something like a @ symbol, so the characters entered in the TextBox will be replaced with that symbol (in this case nobody will see what characters are being entered).
Was This Post Helpful? 0
  • +
  • -

#7 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 02:05 AM

View Postnomeealy, on 31 Dec, 2008 - 12:52 PM, said:

Can anyone tell me how to set a password in the C# application (Windows application) , like whenever I run the program it ask me for the password???

Hi
Usually , we use a login form asking for user login name and password before letting him to use the application.
If that's the case ,It would be a good idea to use an application context to accomplish this task.An ApplicationContext is an object that tells the Application object the order of showing forms to user.It has a MainForm property that you can set to let Application knows which form to show next.
Here there's an example that shows how to use an application context.
Was This Post Helpful? 0
  • +
  • -

#8 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 11:48 AM

@ CORE

Basically the code ur tryin 2 tell is seen in the main form but I want a textbox appearing before the application and user can only allow to access the application if he enters the password correctly ...!!!!
Was This Post Helpful? 0
  • +
  • -

#9 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 11:52 AM

You just have to set up the startup object for your application to your log in form. In the Program.cs file (you can locate it in the Solution Explorer) you can find the line:

			Application.Run(new Form1());



You just have to replace Form1 with the name of your log in form (so it will become thew startup form). Then, you just use the code I showed - if the password is correct, just close the form and open another one. If the password is incorrect, then show a message (or do another action, like exiting the application).
Was This Post Helpful? 0
  • +
  • -

#10 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 12:02 PM

@CORE

Please correct me as am gettin pissed off and my head is gettin blown off.. I have created a Windowsapplication(named example) and make a textbox and button and ur code behind that button then I have gone to the Program.cs of the example and write Application.Run(new binary()); (as i have to run binary program when the user enters the passwd correctly) but the error is showing although i have added binary reference in in ... Correct me please
Was This Post Helpful? 0
  • +
  • -

#11 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 12:08 PM

In the piece of code I have shown (from the Program.cs file) you have to set the name of the Windows Form to be shown at startup, not a name like binary (unless binary is the name of the startup form). And in my first piece of code (verification) you should replace the comments with specific code you want to be executed in each case.
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 12:52 PM

here's how you would do it.

In my example, I have a main form that I want to open AFTER the user logs in correctly. This form is called "frmMain".

I also have a login form called "frmLogin" that will allow a user to login BEFORE the main from is shown.

my Program.cs file:
static class Program
{
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main()
	{
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(false);

		// the form that you put in the following statement
		// is the form that you want to open AFTER the user
		// has logged in successfully.
		// I am opening the main form of my application, because it is the form
		// that I want to open AFTER the user logs in successfully.

		Application.Run(new frmMain());
	}
}



the "frmMain" form's code:
public partial class frmMain : Form
{	
		//Constructor of the main form
	public frmMain()
	{
		InitializeComponent();
   
		// this code runs BEFORE the main form is drawn to the screen
		//  I am opening the login screen to allow the user to login BEFORE
		// the main application opens.

		frmLogin loginForm = new frmLogin();
		loginForm.ShowDialog();
		loginForm.Dispose();   
	}
}



basically, when the application is started, it will start the "frmMain" which calls the constructor for the form. In the constructor, I open the "frmLogin" form.

the "frmLogin" form's code:
public partial class frmLogin : Form
{
	bool authenticated = false;

	public frmLogin()
	{
		InitializeComponent();
	}

	private void btnLogin_Click(object sender, EventArgs e)
	{			   
		if (ValidateLogin(txtUserName.Text, txtPassword.Text))
		{
			authenticated = true;
			this.Close();
		}
		else
		{
			MessageBox.Show("Invalid login.  Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
	}

	private bool ValidateLogin(string userName, string password)
	{
		// do your validation of the login.
		// return true or false depending on whether the login was successful
	}

	private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
	{
		if (!authenticated)
			Application.Exit();
		else
			db.Dispose();
	}
}



I use a username and password to validate the login. You do NOT have to use both if you don't want to. You can use just a password if you like.

This post has been edited by eclipsed4utoo: 01 January 2009 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#13 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 12:57 PM

I think using System.Diagnostics to Start a process is much more efficient and easier..!!!! thanx anyways:-)
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 12:58 PM

View Postnomeealy, on 1 Jan, 2009 - 02:57 PM, said:

I think using System.Diagnostics to Start a process is much more efficient and easier..!!!! thanx anyways:-)


is the application you are opening a .Net project? If so, do you have the source code?

This post has been edited by eclipsed4utoo: 01 January 2009 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#15 nomeealy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 10-October 08

Re: How to set a password in Winform application

Posted 01 January 2009 - 01:13 PM

yes its a .net project
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2