Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: ASP.NET Does Not Encode Cookies in UrlEncode Format by Default


View products that this article applies to.

This article was previously published under Q313282

↑ Back to the top


Symptoms

When you develop ASP.NET applications that share cookies between earlier versions of Microsoft Active Server Pages (ASP) and ASP.NET code, the data may not be encoded or unencoded as expected.

↑ Back to the top


Cause

In earlier versions of Microsoft Active Server Pages (ASP), cookies are encoded in UrlEncode format when they are written, and cookies are unencoded when they are read. However, ASP.NET does not encode or unencode cookies in UrlEncode format by default. As a result, you may encounter unexpected behavior in ASP.NET applications.

↑ Back to the top


More information

Steps to Reproduce the Behavior

  1. In an earlier version of ASP, create a new ASP page, and then add the following code:
    <%@ Language="VBScript"%>
    <%
        Response.Cookies("MyCookie") = "some value and other characters such as #, $, and so on"
        Response.Cookies("MyCookie").Path = "/"
        Response.Write(Request.Cookies("MyCookie"))
    %>
    <body>
    <form id="Form1" method="post" action="CookieTest.aspx">
        <input type="submit" value="Click Me" ID="Submit1" NAME="Submit1">
    </form>
    </body>
    					
  2. Create a new .aspx page named CookieTest.aspx in the same project or application.
  3. Add the following code to the .aspx page:
    <script runat="server" language="vb">
        sub Page_Load()
            Response.Write("Hello from CookieTest.aspx ...<br>")
            Response.Write(Request.Cookies("MyCookie").Value() + "<br>")
        end sub
    </script>
    					
  4. Run the ASP page first. Take note of the output.
  5. Click Submit1 to browse to CookieTest.aspx. Notice that the output is encoded in UrlEncode format. This occurs because the ASP code from the previous page sets the cookie's data.
  6. To decode the data that is retrieved from the cookie, use the following syntax:
    Response.Write(Server.UrlDecode(Request.Cookies("MyCookie").Value()))
    					

↑ Back to the top


References

For additional information about ASP.NET articles and resources, click the article number below to view the article in the Microsoft Knowledge Base:
305140� INFO: ASP.NET Roadmap
For additional information about state management in ASP.NET, click the article number below to view the article in the Microsoft Knowledge Base:
307598� INFO: ASP.NET State Management Overview
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
302390� HOW TO: Use Cookies in an ASP Page
For more information, refer to the following Microsoft Web sites:

↑ Back to the top


Keywords: KB313282, kbstate, kbprb, kbcookie

↑ Back to the top

Article Info
Article ID : 313282
Revision : 9
Created on : 2/23/2007
Published on : 2/23/2007
Exists online : False
Views : 541