Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

c# Tutorial : How write a password protected application.


  • Please log in to reply
17 replies to this topic

#1 yoda174

yoda174

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 22 May 2010 - 10:08 AM

In this tutorial i will show you what can we write an application that protected with username and password. Let's start.

First we make a project.
Then we make the design of the loging form.(we required 2 textbox that called txtUser,and txtPass, 1 checkbox, 1 button that I called btnLogin and some label.)
That's mine :
Posted Image
then we can start the designing of the main form( the main form we can see after logging in)

The code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace password_protect_application
{
    public partial class Form1 : Form
    {
        Form2 mainform = new Form2();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //the checking method
            //you can put into the bracket the password,and username what you want
            if (txtUser.Text.ToString().CompareTo("test") == 0 & txtPass.Text.ToString().CompareTo("test") == 0)
            {
                MessageBox.Show("Login Sucessfull.Click OK to continue", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();     //hide the login form
                mainform.Show(); //& show the main form 
            }
            else
            {
                MessageBox.Show("Wrong Username/Password! \n Please try again!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPass.Text = ""; //delete writed password
                txtUser.Text = ""; //delete writed username
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                txtPass.PasswordChar = '*'; //set passwordchar to '*'
                checkBox1.Text = "Show password"; //change checkbox text to show password
            }
            else 
            {
                txtPass.PasswordChar = (char)0; //reset passwordchar to default
                checkBox1.Text = "Hide password"; //change password to hide password
            }
        }

    }
}


This source code content a password hide/show function also :)
I Hope that it was very helpfull and if you have any question or problem please ask me and i reply:)

P.S : Sorry for my bad English but I live in Hungary and english isn't my mother language.

Edited by yoda174, 17 June 2010 - 02:25 AM.

  • 0

#2 mbridges

mbridges

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 20 June 2010 - 05:41 PM

Nice tutorial! Thanks!
  • 0

#3 bishisht

bishisht

    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 27 June 2010 - 11:28 PM

can we implement this login/logout process by querying the username and passwords from sql server...if yes please let me know
  • 0

#4 yoda174

yoda174

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 28 June 2010 - 01:29 AM

In my opinion it is possible... I try implement and if work i send you a pm.:thumbup1:
  • 0

#5 bishisht

bishisht

    CC Newcomer

  • Member
  • PipPip
  • 23 posts

Posted 28 June 2010 - 06:15 AM

ok thanks
  • 0

#6 gludewig

gludewig

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 06 July 2010 - 07:55 AM

I'd also be interested in knowing how to do this when the usernames and passwords are stored in an SQL Server database.
  • 0

#7 gacanepa

gacanepa

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 18 May 2011 - 08:21 AM

Great post! I'm a C# newbie from Argentina.
Did you find a way to make it work using an user name / password stored in a sql database? If so, would you be as kind as to share it?
Thanks in advance and keep up the good work!
  • 0

#8 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 18 May 2011 - 08:59 AM

@yoda174

you can upload your zip file of your project (for implementing SQL query inside login form) on this site
  • 0

#9 yoda174

yoda174

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 18 May 2011 - 11:58 AM

Ok. I work on the project and if it will be completted i will publicate it :D
  • 0

#10 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 25 May 2011 - 10:44 AM

I posted a tutorial a while ago on one option of using usernames and passwords in databases.
http://forum.codecal...-passwords.html
  • 0

#11 Overkill

Overkill

    CC Lurker

  • New Member
  • Pip
  • 9 posts

Posted 09 June 2011 - 05:14 AM

Thank you, this is just what i needed :thumbup:
  • 0

#12 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 09 June 2011 - 05:34 AM

your most certainly welcome!
  • 0