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

ASP.NET Session Login Without Cookies

Rate me:
Please Sign up or sign in to vote.
2.65/5 (46 votes)
11 Aug 2003 304.7K   74   28
Easiest way to authenticate users without cookies.

Introduction

The following describes the easiest way I have found to force users to log into an ASP.NET website for each session but not require them to accept cookies. You must do the following things.

  1. Create a Web.config file with the appropriate entries to allow session state management.
  2. Create a well formed Global.asax file with the code below included in it.
  3. Create a login page to authenticate users against a database or whatever method you desire.

Code usage

Required Code in the Global.asax

VB.NET
' Fires when the session is started and sets the default loggedin state to ""
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Session("Loggedin") = ""
CheckLoggedIn()
End Sub
VB.NET
' Called when the request has been process by the Request Handler and 
' HttpSessionState is available [This is the key piece of code that forces 
' the user is login check with each page request]
Sub Application_OnPostRequestHandlerExecute()
CheckLoggedIn()
End Sub
VB.NET
'Check that the user is logged in.
Sub CheckLoggedIn()
'If the user is not logged in and you are not currently on the Login Page.
If Session("LoggedIn") = "" And InStr(Request.RawUrl, "Login.aspx") = 0 Then
Response.Redirect("~/Login/Login.aspx")
End If
End Sub

Finally create a Login.aspx file that authenticates the user. If the user is allowed in, set: Session("Loggedin") = "Yes"

That's all there is to it. Hope this helps! Enjoy!

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCookieless = no good Pin
dog_spawn13-Aug-03 5:28
dog_spawn13-Aug-03 5:28 
GeneralRe: Cookieless = no good Pin
Philip Patrick20-Aug-03 20:28
professionalPhilip Patrick20-Aug-03 20:28 
GeneralRe: Cookieless = no good Pin
dog_spawn20-Aug-03 21:33
dog_spawn20-Aug-03 21:33 
GeneralRe: Cookieless = no good Pin
DaveAuld30-Aug-03 16:54
professionalDaveAuld30-Aug-03 16:54 
GeneralYes Pin
dog_spawn31-Aug-03 3:35
dog_spawn31-Aug-03 3:35 
GeneralRe: Not exactly Pin
Daniel Fisher (lennybacon)19-Aug-03 6:38
Daniel Fisher (lennybacon)19-Aug-03 6:38 
GeneralRe: Not exactly Pin
Chris Maunder20-Aug-03 16:59
cofounderChris Maunder20-Aug-03 16:59 
GeneralRe: Not exactly Pin
Philip Patrick20-Aug-03 20:30
professionalPhilip Patrick20-Aug-03 20:30 
Yeah, reading about it, looks fantastic Smile | :) ASP.NET turned to be the best server scripting lang, more I "play" with it, more I like it Smile | :)

Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
GeneralRe: Not exactly Pin
Anonymous19-Sep-05 20:54
Anonymous19-Sep-05 20:54 

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.