Click here to Skip to main content
12,944,769 members (50,829 online)
Click here to Skip to main content
Add your own
alternative version


114 bookmarked
Posted 24 Aug 2002

Cookieless ASP.NET forms authentication

, 25 Aug 2002
Rate this:
Please Sign up or sign in to vote.
They say it is not possible to use cookieless forms authentication in .NET. Well it is, and relatively easy to accomplish!

Cookieless forms authentication

Why, when?

They say, its not possible. Well it is, and relatively easy to accomplish!

Lot of companies and people want to exclude cookie usage from their lives. Partly because its said to be insecure, partly because they see no reason to use it.

In my case, it was mandatory not to use cookies, but make a forms login page. Of course I've started with the normal forms authentication, cause I believed, that the big brother couldn't make such a mistake, to use cookies.

They did. After searching all the forums how to skip cookie usage, all I've found was this:

The hard way

If you pass the encoded cookie as a GET parameter to the Response.Redirect() function, the system will work as normal: the user will be signed in until the parser can find the cookie as a GET parameter, or a cookie is not easy, and makes no sense at all.

The code snippet to accomplish the "get" way of cookieless authentication is:

FormsAuthenticationTicket tkt;
string cookiestr;
HttpCookie ck;

//create a valid ticket for forms authentication
tkt = new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.Now.AddMinutes(30), false, "your custom data");<BR>

//get the string representation of the ticket
cookiestr = FormsAuthentication.Encrypt(tkt);

//redirect to the return URL using the cookie in the address field
//In the web.config, we called out auth. ASPXFORMSAUTH2, so set that value
string strRedirect = Request["ReturnUrl"] + "?.ASPXFORMSAUTH2=" + cookiestr;
Response.Redirect(strRedirect, true);

This is useless, I tell you. Completely unpleasant, and insecure (you have to change all the links, which of course you won't)

And here is the way, you can do it:

The configuration

No authentication tag needed beside the "none". The next line in the web.config will tell the framework not to store the session ID in a cookie, but add as a special directory to the address field.

<sessionState cookieless="true" timeout="20" />

After adding this line, the address field will always look like:


As you can see, on each request, the session ID is passed as a directory. Very smart solution from MS! When you want to create a link with get parameters to another page, you have to pay attention to it, since calling an aspx without the session ID in the address will create a new session. So, to create a link, that has GET parameters, do this:

string url =
// we build the whole link. Firstly, we get our host name
 "http://" + Request.Headers["Host"] + "/" + 
// then the path of the request, and append the session ID, as shown above
 Request.ApplicationPath + 
 "/(" + Session.SessionID + 
// simply add the target page with the HTTP-GET parameters.
 "State", state.ToString(),
 "Lang", langID.ToString()

(OK, I needed it. Usually people don't care about GET parameters, so probably you won't need it.)

The coding part

In global.asax.cs, add:

private void InitializeComponent()
{  // This tells the global to catch all session initialization events,
   // So before every page load, we will have the Global_Acq. called! Good starting!
   this.AcquireRequestState += new
void Global_AcquireRequestState(object sender, System.EventArgs e)
	//This tells the global to check whether code "Name-John" is in the session 
	//variable, called "Authenticated". To say it simple, 
	//checks, whether someone set this 
	if((string)Session["Authenticated"] != "Name-John")
	// If yes, do nothing, so the requested page will load.
		// If it's not set yet, redirect to the login page, 
		// if the caller is not the login page already. If it is, we don't 
		//want loops, so let is load

If the user entered valid codes (check them however you like), in login.apsx.cs, set the session variable Authenticated to code Name-John, so the global will let the users download pages.

Session["Authenticated"] = "Name-John";
//the auth is successfull, so send the user to the page
Response.Redirect("default.aspx", true);

As you see, this is a pure redirect function. No ASP.NET forms authentication is used. On the default.aspx, place whatever you want. Those controls will be in safety. If you want to sign out the user, call this code:

//signs out
//redirects to itself. This will redirect to login.aspx, cos we are signed out

Misc good to knows

After clicking the sign-out, the user will be back on login.aspx. If he presses back, he can see the page from his browser's cache, but cannot click anything. It could be wise to set the cache expiration.

If you press [Back], then [Refresh], the explorer will asks for "The page cannot be refreshed without resending the information", and prompts for "Retry/Cancel".

Usually, when someone presses retry, the password is sent again, and the user is signed in again. Well, not in our case

You can try, that this method really doesn't use cookies: in Internet Explorer, go Tools / Internet Options. Go Privacy, and block all cookies, then try to sign in'n'out.

If you have any questions/comments, please send it to me!

Sincerely, Adam


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

Hungary Hungary
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThat's the wrong way. Pin
Member 37673541-Jul-08 10:20
memberMember 37673541-Jul-08 10:20 
That's the wrong way, the only changes that you need to make to your application in order to use cookieless session and cookieless authorization are the following:

1. On the web.config, set the authentication to Forms, set the cookieless attribute of the element to UseUri, set loginUrl to the url of your login page, and defaultUrl to your main page or the name of the page where you want to be redirected after loging in (unless you got to the login page by being redirected from a secured page, in that case you will be sent back to the page where you came from), and finally set the sessionState cookieless property to UseUri, all this can be done by simply adding the following lines to the <system.web> section of your web.config.

<br />
      <authentication mode="Forms"><br />
        <forms cookieless ="UseUri" loginUrl="login.aspx" defaultUrl="default.aspx" /><br />
      </authentication><br />
<br />
      <sessionState cookieless="UseUri" /><br />

2. On you code you need to modify all your redirects and hyperlinks (unless the hyperlinks are webcontrols from the .NET FCL, those format the url automatically for you). This is as easy as calling Response.ApplyAppPathModifier("~/myvirtualurl.aspx") or Page.ResolveClientUrl("~/myvirtualurl"). It is a good practice to always do this even if you're not using cookieless session or authentication, specially when developing controls or reusable code, that way your code will support cookieless session and authorization without modifications by just changing the web.config file.

3. If you implemented forms authentication correctly using cookies, then those are the only changes required to use cookieless session and authentication, that way both the sessionID and the FormsAuthenticationTicket will be encoded in the url as a folder name. By implementing forms authentication correctly I mean that:

A. you used FormsAuthentication.SetAuthCookie to set the authentication ticket (this method has a very misleading name since it's also used for setting the ticket on the Uri when using cookieless authentication),

B. you used FormsAuthentication.SignOut to remove the ticket, and

C. You used the autherization tags on the web.config to configure what kind of users have access to your page, this needs to be done on <location> tags for individual pages and on the <system.web> tag for all pages that don't have a <location> tag on the web.config.

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 26 Aug 2002
Article Copyright 2002 by brutal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid