Click here to Skip to main content
15,031,941 members
Articles / Web Development / ASP.NET
Posted 28 Nov 2007


21 bookmarked

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

Comments and Discussions

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