Click here to Skip to main content
15,884,042 members
Articles / Web Development / ASP.NET
Article

Easy way to create secure ASP.NET login using Session

Rate me:
Please Sign up or sign in to vote.
2.06/5 (12 votes)
28 Nov 2007 107.8K   4.1K   21   16
It an easy and secure way for begginer ASP.NET developer to create secure login WebForms using Sessions

Introduction

As an ASP.NET developer, I needed to create a secure login WebForm, but I also needed it to be easy to implement, not so complicated, and also accomplish the mission

Sessions

Sessions are a real important thing, that used in many things we can't even imagine. You can use sessions in passing variables between WebForms instead of query-strings when passing secure values, it can also be used to create secure, and easy login.

Defining our Sessions

In the file "Global.asax", We'll define our sessions in the function "Session_Start()"

protected void Session_Start(Object sender, EventArgs e)
{
     //The first Session "Logged" which is an indicator to the
     //status of the user
     Session["Logged"]="No";
     //The second Session "User" stores the name of the current user
     Session["User"]="";

     //The third Session "URL" stores the URL of the
     //requested WebForm before Logging In
     Session["URL"]="Default.aspx";
}

In the "Page_Load" of the Secured files, or the files which needed the user first to LogIn before seeing them, we add just check if the user is Logged or not.

private void Page_Load(object sender, System.EventArgs e)
{
    if(Session["Logged"].Equals("No"))
    {
         ....
    }
    else
    {
         ....
    }
}

In the "Login.aspx" file, I've made checking the UserName and password with a regular if condition, with no database usage. But this code can be modified easily to check the Username and Password from a table in a database

if(UserNametxt.Text.Trim()=="Abdallah" && Passwordtxt.Text.Trim()=="Fayez")
{
    ....
}

else
{
    ....
}

The Code is available to download, with a sample "Login.aspx", and "Default.aspx" WebForms which make it easier for you to modify the code in best shape for you.

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


Written By
Software Developer (Senior)
United States United States
I'm working in software development since my childhood, I used Logo, Basic, qBasic, Fortran 77, Visual Basic, C, C++, finally C# and many various mainly web technologies

Comments and Discussions

 
QuestionLogin credentials change Pin
Member 1489148916-Jul-20 11:11
Member 1489148916-Jul-20 11:11 
Questionis it secure Pin
tamdata t30-Apr-13 21:25
tamdata t30-Apr-13 21:25 
AnswerRe: is it secure Pin
Abdallah Fayez7-Jan-14 14:56
Abdallah Fayez7-Jan-14 14:56 
GeneralVery Nice Pin
VikZ9125-Mar-13 11:32
VikZ9125-Mar-13 11:32 
GeneralMy vote of 5 Pin
erhardeep17-Feb-12 3:26
erhardeep17-Feb-12 3:26 
GeneralRe: My vote of 5 Pin
Abdallah Fayez16-Dec-12 23:34
Abdallah Fayez16-Dec-12 23:34 
Generalجزاك الله Pin
Member 822963221-Jan-12 20:16
Member 822963221-Jan-12 20:16 
Thanks brother. It worked for me.
Hemn, Kurdistan - Iraq
GeneralRe: جزاك الله Pin
Abdallah Fayez16-Dec-12 23:34
Abdallah Fayez16-Dec-12 23:34 
GeneralCode to use a database to validate user Pin
Rosita5-Dec-07 12:02
Rosita5-Dec-07 12:02 
AnswerRe: Code to use a database to validate user Pin
Abdallah Fayez30-Dec-07 11:12
Abdallah Fayez30-Dec-07 11:12 
QuestionWhy can't we use <authorization> section of configuration file? Pin
Member 39047285-Dec-07 4:51
Member 39047285-Dec-07 4:51 
AnswerRe: Why can't we use <authorization> section of configuration file? [modified] Pin
Abdallah Fayez30-Dec-07 10:32
Abdallah Fayez30-Dec-07 10:32 
GeneralworksNice start Pin
Islam Khalil Saber29-Nov-07 3:14
Islam Khalil Saber29-Nov-07 3:14 
GeneralRe: worksNice start Pin
Abdallah Fayez29-Nov-07 3:20
Abdallah Fayez29-Nov-07 3:20 
Generalallah 3alek Pin
el3omda28-Nov-07 6:31
el3omda28-Nov-07 6:31 
GeneralRe: allah 3alek Pin
Abdallah Fayez29-Nov-07 3:22
Abdallah Fayez29-Nov-07 3:22 

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.