IIS/ASP. NET cookieless support not working as expected

Source: Internet
Author: User

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







<% = request. rawurl %> <% = request. headers ["aspfiltersessionid"] %> <% = session. sessionid %>
raw URL
cookiless cookies
aspfiltersessionid request HTTP header
session ID


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.