Http://weblogs.asp.net/paulomorgado/archive/2008/08/01/iis-asp-net-cookieless-support-not-working-as-expected.aspx
IIS/ASP. NET cookieless support not working as expected
In one of the environments I work, Cookies cannot be used because the pages run inside Web browser controls running on a client application and cookies end up being shared by all browsers.
Fortunately, ASP. NET allows us to persist some cookies as part of the URL.
To persist the session state identifier cookie in the URL we just need to add the following configuration:
<Configuration>
<System. Web>
<Sessionstate cookieless = "useuri"/>
</System. Web>
</Configuration>
And you'll get URLs like this:
Http: // localhost/cookieless/(S (jcmwek3ja0lvdpbwoacpjirv)/default. aspx
The way IIS and ASP. net do this is by IIS removing the section between parenthesis after the virtual directory path and adding the aspfiltersessionid HTTP header to the request. than, Asp. net picks it up and extracts the cookie.
I wrote this simple page to demonstrate this working:
<% @ page Language = "C #" autoeventwireup = "true" %>
raw URL | <% = request. rawurl %>
cookiless cookies aspfiltersessionid request HTTP header | <% = request. headers ["aspfiltersessionid"] %>
session ID | <% = session. sessionid %>
For the above URL, we'll get a page like this:
Raw URL/cookieless/default. aspx
Cookiless cookies
Aspfiltersessionid request HTTP header S (jcmwek3ja0lvdpbwoacpjirv)
Session ID jcmwek3ja0lvdpbwoacpjirv
IIS strips these cookies even for serving static content like cascading stylesheets.
You can test this by creating a default theme. You can do this by adding a default folder under the app_themes folder and adding a styles.css file to it:
Body {background-color: yellow;} table, TR, TD {border: thin solid black ;}
And setting the theme as default using the following Configuration:
<Configuration>
<System. Web>
<Sessionstate cookieless = "useuri"/>
<Pages theme = "default"/>
</System. Web>
</Configuration>
And you'll get a "pretier" Page:
Raw URL/cookieless/default. aspx
Cookiless cookies
Aspfiltersessionid request HTTP header S (jcmwek3ja0lvdpbwoacpjirv)
Session ID jcmwek3ja0lvdpbwoacpjirv
If you have special needs for your session state identifiers, You can implement your own session identifier manager.
But if you want to use cookieless cookies, you only have one way to do it: extend the sessionidmanager class:
Public class sessionidmanager: system. Web. sessionstate. sessionidmanager
{
Public override string createsessionid (system. Web. httpcontext context)
{String id = system. guid. newguid (). tostring ("B"); Return ID ;}
Public override bool validate (string ID)
{Try {new system. GUID (ID); Return true ;}catch {return false ;}}
}
And configure the session state module to use it:
<Configuration> <system. Web>
<Sessionstate cookieless = "useuri" sessionidmanagertype = "sessionidmanager"/>
<Pages theme = "default"/>
</System. Web> </configuration>
And we'll end up with this nice page:
Http: // localhost/cookieless/(S (% 7b0861e55a-e29b-4b6f-825b-1e1d4369f095% 7D)/default. aspx
Raw URL/cookieless/(S ({0861e55a-e29b-4b6f-825b-1e1d4366f095})/default. aspx
Cookiless cookies
Aspfiltersessionid request HTTP Header
Session ID {0861e55a-e29b-4b6f-825b-1e1d4379f095}
Oops! What happened here?
Looks like IIS was unable to transfer the cookies to the appropriate HTTP header but ASP. net was able to find the requested resource. on the other hand, IIS couldn't find the http: // localhost/cookieless/(S (% seconds % 7D) app_themes/default/styles.css.
This happens in these environments:
Operating System iis asp. NET
Windows xp pro SP3 5.1 2.0 SP1, 3.5
Windows Server 2003 R2 6 2.0 SP1, 3.5
Windows Server 2008 7 2.0 SP1, 3.5
Fortunately, in IIS 7 you can have HTTP modules in integrated pipeline mode that are called for every resource requested to IIS.
Your module doesn't even need to do nothing. It just needs to exist:
Public class module: system. Web. ihttpmodule {
Public void dispose (){}
Public void Init (system. Web. httpapplication context ){}
}
And be added to the Configuration:
<Configuration>
<System. Web>
<Sessionstate cookieless = "useuri" sessionidmanagertype = "sessionidmanager"/>
<Pages theme = "default"/>
</System. Web>
<System. webserver>
<Modules>
<Add name = "module" precondition = "integratedmode" type = "module"/>
</Modules>
</System. webserver>
</Configuration>
And our "pretty" page is back:
Raw URL/cookieless/default. aspx
Cookiless cookies
Aspfiltersessionid request HTTP header S ({0861e55a-e29b-4b6f-825b-1e1d4366f095 })
Session ID {0861e55a-e29b-4b6f-825b-1e1d4379f095}
Is it just me, or there's something definitely wrong here?
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/weiyue_net/archive/2010/03/18/5392096.aspx