Jump to content

c# Tutorial : How write a password protected application.

- - - - -

  • Please log in to reply
14 replies to this topic

#1
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
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.


#2
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Nice tutorial! Thanks!

#3
bishisht

bishisht

    Newbie

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

#4
yoda174

yoda174

    Newbie

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

#5
bishisht

bishisht

    Newbie

  • Members
  • PipPip
  • 23 posts
ok thanks

#6
gludewig

gludewig

    Newbie

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

#7
gacanepa

gacanepa

    Newbie

  • Members
  • PipPip
  • 19 posts
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!

#8
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 474 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
@yoda174

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

#9
yoda174

yoda174

    Newbie

  • Members
  • PipPip
  • 25 posts
Ok. I work on the project and if it will be completted i will publicate it :D

#10
sam_coder

sam_coder

    Programming Expert

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

#11
Overkill

Overkill

    Newbie

  • Members
  • PipPip
  • 10 posts
Thank you, this is just what i needed :thumbup:

#12
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
your most certainly welcome!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users