Click here to Skip to main content
11,929,166 members (64,901 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked

Easy way to create secure ASP.NET login using Session

, 28 Nov 2007
Rate this:
Please Sign up or sign in to vote.
It an easy and secure way for begginer ASP.NET developer to create secure login WebForms using Sessions


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 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
     //The second Session "User" stores the name of the current user

     //The third Session "URL" stores the URL of the
     //requested WebForm before Logging In

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)

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")


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.


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


About the Author

Abdallah Fayez
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

You may also be interested in...

Comments and Discussions

Questionis it secure Pin
tamdata t30-Apr-13 22:25
membertamdata t30-Apr-13 22:25 
hi all
its a possible way . but is it secure to use session for check in another page?
session hijackers is in Ambush!
what is your solution to prevent it?
AnswerRe: is it secure Pin
Abdallah Fayez7-Jan-14 15:56
memberAbdallah Fayez7-Jan-14 15:56 
GeneralVery Nice Pin
VikZ9125-Mar-13 12:32
memberVikZ9125-Mar-13 12:32 
GeneralMy vote of 5 Pin
erhardeep17-Feb-12 4:26
membererhardeep17-Feb-12 4:26 
GeneralRe: My vote of 5 Pin
Abdallah Fayez17-Dec-12 0:34
memberAbdallah Fayez17-Dec-12 0:34 
Generalجزاك الله Pin
Member 822963221-Jan-12 21:16
memberMember 822963221-Jan-12 21:16 
GeneralRe: جزاك الله Pin
Abdallah Fayez17-Dec-12 0:34
memberAbdallah Fayez17-Dec-12 0:34 
GeneralCode to use a database to validate user Pin
Rosita5-Dec-07 13:02
memberRosita5-Dec-07 13:02 
AnswerRe: Code to use a database to validate user Pin
Abdallah Fayez30-Dec-07 12:12
memberAbdallah Fayez30-Dec-07 12:12 
QuestionWhy can't we use <authorization> section of configuration file? Pin
Member 39047285-Dec-07 5:51
memberMember 39047285-Dec-07 5:51 
AnswerRe: Why can't we use <authorization> section of configuration file? [modified] Pin
Abdallah Fayez30-Dec-07 11:32
memberAbdallah Fayez30-Dec-07 11:32 
GeneralworksNice start Pin
Islam Khalil Saber29-Nov-07 4:14
memberIslam Khalil Saber29-Nov-07 4:14 
GeneralRe: worksNice start Pin
Abdallah Fayez29-Nov-07 4:20
memberAbdallah Fayez29-Nov-07 4:20 
Generalallah 3alek Pin
el3omda28-Nov-07 7:31
memberel3omda28-Nov-07 7:31 
GeneralRe: allah 3alek Pin
Abdallah Fayez29-Nov-07 4:22
memberAbdallah Fayez29-Nov-07 4: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 28 Nov 2007
Article Copyright 2007 by Abdallah Fayez
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid