Click here to Skip to main content
12,508,185 members (54,662 online)
Click here to Skip to main content
Add your own
alternative version

Stats

154.6K views
706 downloads
72 bookmarked
Posted

How to implement Two basics uses for the Asp.net Login control 2.0 (login and RememberMe)

, 23 Nov 2006
Rate this:
Please Sign up or sign in to vote.
This article will show you how to implement two basics uses of the ASP.NET Login control (2.0), The login task itself to validate the users who’s trying to access your web site. In addition i will show how to implement the REMEMBERME process using cookies to save the user name inside the user PC

Introduction

The login control is a good gift to save time in the way to make one of the most important tasks everytime that you developed a site, the login task (Who’s inside, Who’s outside)  Inside the next lines I will try give you a basic, but functional login control programming, which one you can use in your project.

The end of this program is to show the login page first (there is the login control) the user insert their login credentials (username, password) the page evaluates the credentials against a database, if the user exist and the password is the same saved onto database, the login page grant the access to welcome page, at negative case the login page is refresh showing a Login error message and set ready to attempt again.

 

 

 

This code on Login control authenticate process evaluates the users credentials and grant or deny the access to the destination page.

 

 

protected void Logeo_Authenticate(object sender, AuthenticateEventArgs e)
{
        try
        {
            string id_user = Logeo.UserName.Trim(); //Get the username from the control
            string pass_user = Logeo.Password.Trim(); //get the Password from the control

            

 //These are a sql and connection Examples

 string sql = "SELECT coduser, nameuser FROM users WHERE iduser = @param_Id AND passuser = @param_Password";
 
            public SqlConnection conexionExpress = new SqlConnection("server='localhost\\SQLExpress';Integrated Security=true;Initial Catalog=college;User Instance=false"); //Create the server connection

  
            SqlCommand comandoSql = new SqlCommand(sql, conexionExpress); //Create the sql command using sql string and sql connection

//Add the sql parameters

  comandoSql.Parameters.AddWithValue("@param_Id", id_user); //New on VS 2.0

  comandoSql.Parameters.AddWithValue("@param_Password", pass_user);


            string cod_user = "";
            string name_user = "";
            //Open database connection
                        
            conexionExpress.Open();
            SqlDataReader dr = comandoSql.ExecuteReader();
            while (dr.Read())
            {
                cod_user = dr.GetValue(0).ToString(); //The coduser is unique onto database table
                name_user = dr.GetValue(1).ToString();
            }
            conexionExpress.Close();//Close Database Connection
            if (cod_user != "") //The user exist onto database
            {
                /*Create the session vars
                Session["coduser"] = cod_user;
                Session["nameuser"] = name_user;
                e.Authenticated = true; //Grant the access, Goes to DestinationPageUrl
            }
        }
        catch(Exception)//On Login Error
        {
            e.Authenticated = false;//Confirm that you are out

  conexionExpress.Close();//On any error case, close the database connection
        }

}

Now we will change the welcome page to show the user name who grant the access

 

On welcome.aspx modify the load event

 

protected void Page_Load(object sender, EventArgs e)

 {

        if (!IsPostBack)

        {

            Message.Text = "Welcome " + Session["nameuser"].ToString() + " Your user code is " +  Session["coduser"].ToString();

        }

 }

 

At this point you already do the login event in you website, but we have a problem, ¿what happened if the user write the absolute url to welcome page?, he will reach the page, so lets add these lines at web.config file in order to redirect all the attempts to visit forms using absolute urls to login form.

Change the authentication mode to Forms and add the next in order to lock your site, and last define the login form URL like the default form.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Britney S. Morales
Web Developer
Colombia Colombia
Web Developer during 3 years and counting, i worked over my school website by two years, right now im webmaster at World Wowan foundation banking in my home city.

Please forgive my pictures examples, they are at spanish language because its my mother language.


You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionCode file not found Pin
tahirgr8_20007-Jul-14 19:58
membertahirgr8_20007-Jul-14 19:58 
QuestionHi Pin
pagani zonda21-Oct-13 20:44
grouppagani zonda21-Oct-13 20:44 
GeneralMy vote of 5 Pin
Heaven202017-Mar-13 5:40
memberHeaven202017-Mar-13 5:40 
GeneralMy vote of 2 Pin
Marco Bertschi6-Feb-13 4:00
memberMarco Bertschi6-Feb-13 4:00 
QuestionGood Example Pin
Alireza_13622-Oct-12 4:11
memberAlireza_13622-Oct-12 4:11 
GeneralNeed Code for Login control with Remember me option in VB.NET Pin
saleforu14-Jul-09 7:52
membersaleforu14-Jul-09 7:52 
GeneralMy vote of 1 Pin
snorkie11-Jan-09 15:30
membersnorkie11-Jan-09 15:30 
GeneralDynamically Breadcrumb bar Pin
kkpaliwal13-Jun-08 3:08
memberkkpaliwal13-Jun-08 3:08 
GeneralRe: Dynamically Breadcrumb bar Pin
Britney S. Morales13-Jun-08 4:21
memberBritney S. Morales13-Jun-08 4:21 
GeneralComentario Pin
Member 249133916-Apr-08 3:53
memberMember 249133916-Apr-08 3:53 
Hola Britney, muy bueno el código, la verdad es muy útil. Pero además me impactaste vos, sos preciosa.

Andrés
GeneralRe: Comentario Pin
Britney S. Morales13-Jun-08 4:23
memberBritney S. Morales13-Jun-08 4:23 
GeneralRe: Comentario Pin
.net GuruKool1-Oct-08 0:24
member.net GuruKool1-Oct-08 0:24 
QuestionCant make the C# version works Pin
John Kh14-Apr-08 21:45
memberJohn Kh14-Apr-08 21:45 
AnswerRe: Cant make the C# version works Pin
Britney S. Morales13-Jun-08 4:24
memberBritney S. Morales13-Jun-08 4:24 
GeneralAbout you and your code Pin
Dobaria Neerav1-Apr-08 8:06
memberDobaria Neerav1-Apr-08 8:06 
GeneralRe: About you and your code Pin
Britney S. Morales1-Apr-08 13:31
memberBritney S. Morales1-Apr-08 13:31 
QuestionLogin for ASP.Net Pin
Member 106126828-Feb-08 19:44
memberMember 106126828-Feb-08 19:44 
AnswerRe: Login for ASP.Net Pin
Britney S. Morales13-Jun-08 4:27
memberBritney S. Morales13-Jun-08 4:27 
GeneralHi, Pin
sdev26-Feb-08 6:57
membersdev26-Feb-08 6:57 
QuestionPassword cannot be assigned to -- it is read only Pin
pinxu9-Oct-07 22:06
memberpinxu9-Oct-07 22:06 
AnswerRe: Password cannot be assigned to -- it is read only Pin
SrikanthSapa21-Apr-10 1:30
memberSrikanthSapa21-Apr-10 1:30 
Generalavoid encrypted url Pin
Edward1115-Oct-07 7:50
memberEdward1115-Oct-07 7:50 
GeneralLocalized version of login control Pin
vdhguy4-Oct-07 22:39
membervdhguy4-Oct-07 22:39 
GeneralRemember Me problem Pin
MaverickAjith4-Oct-07 0:52
memberMaverickAjith4-Oct-07 0:52 
GeneralBrowser Closed event Pin
teeboss3-Oct-07 4:10
memberteeboss3-Oct-07 4:10 
QuestionA lil help pls Pin
teeboss28-Sep-07 20:41
memberteeboss28-Sep-07 20:41 
AnswerRe: A lil help pls [modified] Pin
Britney S. Morales29-Sep-07 4:10
memberBritney S. Morales29-Sep-07 4:10 
GeneralRe: A lil help pls Pin
teeboss29-Sep-07 23:01
memberteeboss29-Sep-07 23:01 
GeneralRe: A lil help pls(....) Pin
Aswattha16-Dec-08 19:04
memberAswattha16-Dec-08 19:04 
Generalproblem..plz help Pin
Sujit Mandal.27-Sep-07 23:00
memberSujit Mandal.27-Sep-07 23:00 
GeneralRe: problem..plz help Pin
Britney S. Morales28-Sep-07 2:28
memberBritney S. Morales28-Sep-07 2:28 
Generalvery good article Pin
Sujit Mandal.27-Sep-07 22:14
memberSujit Mandal.27-Sep-07 22:14 
GeneralRe: very good article Pin
Britney S. Morales28-Sep-07 2:36
memberBritney S. Morales28-Sep-07 2:36 
QuestionSQL table design? Pin
darkknight123421-Sep-07 10:10
memberdarkknight123421-Sep-07 10:10 
AnswerRe: SQL table design? Pin
Britney S. Morales28-Sep-07 2:46
memberBritney S. Morales28-Sep-07 2:46 
GeneralRe: SQL table design? Pin
darkknight123428-Sep-07 2:59
memberdarkknight123428-Sep-07 2:59 
Generalnice article... Pin
jaiminpatel96313-Sep-07 0:21
memberjaiminpatel96313-Sep-07 0:21 
GeneralRe: nice article... Pin
Britney S. Morales28-Sep-07 2:52
memberBritney S. Morales28-Sep-07 2:52 
GeneralThank you! Pin
G.Stanley John28-Aug-07 20:46
memberG.Stanley John28-Aug-07 20:46 
GeneralRe: Thank you! Pin
Britney S. Morales28-Sep-07 2:52
memberBritney S. Morales28-Sep-07 2:52 
GeneralHI Pin
maram20002-Aug-07 14:47
membermaram20002-Aug-07 14:47 
GeneralRe: HI Pin
Britney S. Morales3-Aug-07 3:06
memberBritney S. Morales3-Aug-07 3:06 
GeneralHelp me Pin
maram20002-Aug-07 14:40
membermaram20002-Aug-07 14:40 
GeneralRe: Help me Pin
Britney S. Morales3-Aug-07 3:02
memberBritney S. Morales3-Aug-07 3:02 
GeneralHola qué tal ! Pin
JulioDeLeon23-Jul-07 11:15
memberJulioDeLeon23-Jul-07 11:15 
GeneralRe: Hola qué tal ! Pin
Britney S. Morales24-Jul-07 3:05
memberBritney S. Morales24-Jul-07 3:05 
GeneralRe: Hola qué tal ! Pin
JulioDeLeon24-Jul-07 15:10
memberJulioDeLeon24-Jul-07 15:10 
GeneralHi Pin
ar_monti10-Jul-07 2:21
memberar_monti10-Jul-07 2:21 
GeneralRe: Hi Pin
Britney S. Morales10-Jul-07 3:25
memberBritney S. Morales10-Jul-07 3:25 
GeneralWorth reading Pin
SMKarten16-Mar-07 10:16
memberSMKarten16-Mar-07 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 23 Nov 2006
Article Copyright 2006 by Britney S. Morales
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid