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

#13 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 09 June 2011 - 05:35 AM

oh wait, you mean THIS one.. lol, I thought you might have been responding to my post./. =)
  • 0

#14 iceman4154

iceman4154

    CC Regular

  • Just Joined
  • PipPipPip
  • 26 posts

Posted 15 June 2011 - 04:24 AM

Yeah with the .net framework querying any type of sql database is quite easy.
Then you just check the entered user name and password with the one that is returned from the sql database, like shown above. If correct, post login and show new form, or step into main program code.
  • 0

#15 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 15 June 2011 - 05:09 AM

typically, passwords wouldn't be stored plain text in a database, and typically you wouldn't pull the password (or password hash ) back into the application. I've seen schemes work like this more often (intentionally simple for readability).

SELECT id FROM users WHERE username = @username AND password_hash = @password_hash

So, this would pull the user id back if the username and the password match. Returning 0 records means the username and password do not match.

of course having the key of the user (id) means you could pull any additional data for that user, like their name, etc. But I've always considered it best practice to give the application as little access to the password data as possible.
  • 0

#16 raver

raver

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 12 May 2014 - 11:10 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 :
login_www.kepfeltoltes.hu_.jpg
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.

 

yoda174, is programming your hobby or your profession?


  • 0

#17 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts
  • Location:West Sussex, England
  • Programming Language:Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Transact-SQL

Posted 27 October 2014 - 07:44 AM

yoda174, is programming your hobby or your profession?

 

I'd hope a hobby. Hardcoding authentication details is not secure at all, but still okay to play around with I guess.


  • 0

#18 Kriss

Kriss

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Norway <3
  • Programming Language:C#, PHP, Visual Basic .NET
  • Learning:Python, Assembly

Posted 20 November 2015 - 04:03 AM

I'd hope a hobby. Hardcoding authentication details is not secure at all, but still okay to play around with I guess.

Then what is secure?


  • 0