Http Client Protocol Issues

If you use any of these solutions, Please let me know so I can track if any of this is useful to you! Thanks! This is an area to share observations I have made working with Http Client Protocols and the associated technologies. I currently work for the Microsoft team that supports the WinInet, WinHTTP and System.Net API's and classes associated with these technologies. This is not a replacement for Microsoft Support, but an area to discuss these technologies. These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified at Microsoft - Information on Terms of Use

Understanding the New WinInet flag: INTERNET_COOKIE_HTTPONLY

There are a couple of new Cookie flags introduced with the Internet Explorer 8 WinInet.dll.  The INTERNET_COOKIE_HTTPONLY flag allows you to read the HttpOnly cookies in your WinInet Code.  This flag is documented here: http://msdn.microsoft.com/en-us/library/aa384714(VS.85).aspx.  As always, I like to see examples of how this flag works!

Here is a sample ASPX page to create some standard and httponly cookies:

aspx code listing for sample (Copy Code):

<%@ Page Language="C#" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        // Create a new HttpCookie.
        HttpCookie myHttpCookie = new HttpCookie("LastVisit", "sometime");
 myHttpCookie.Expires = DateTime.Now.AddYears(1);

        // By default, the HttpOnly property is set to false
        // unless specified otherwise in configuration.

        myHttpCookie.Name = "MyHttpCookie";
 myHttpCookie.Path = "/";
        Response.AppendCookie(myHttpCookie);

        // Show the name of the cookie.
        Response.Write(myHttpCookie.Name);

        // Create an HttpOnly cookie.
        HttpCookie myHttpOnlyCookie = new HttpCookie("LastVisit", "sometime later");

        // Setting the HttpOnly value to true, makes
        // this cookie accessible only to ASP.NET.

        myHttpOnlyCookie.HttpOnly = true;
 myHttpCookie.Expires = DateTime.Now.AddYears(1);
        myHttpOnlyCookie.Name = "MyHttpOnlyCookie";
        Response.AppendCookie(myHttpOnlyCookie);

        // Show the name of the HttpOnly cookie.
        Response.Write(myHttpOnlyCookie.Name);
        Response.Write("jeff");
    }
</script>


<html  >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<script type="text/javascript">
function getCookie(NameOfCookie)
{
    if (document.cookie.length > 0)
{
    begin = document.cookie.indexOf(NameOfCookie+"=");
    if (begin != -1)
   {
    begin += NameOfCookie.length+1;
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));      
      }
  }
return null; 
}
</script>

<script type="text/javascript">

    // This code returns the cookie name.
    alert("Getting HTTP Cookie");
    alert(getCookie("MyHttpCookie"));

    // Because the cookie is set to HttpOnly,
    // this returns null.
    alert("Getting HTTP Only Cookie");
    alert(getCookie("MyHttpOnlyCookie"));

</script>


</body>
</html>

When you run this page you will note InternetExplorer jscript will not allow you to read the value of MyHttpOnlyCookie.  This new flag will allow you to read that cookie from code however!

To investigate this I decided to use my favorite sample 'httpauth' from the Platform SDK. 

I added this code to the end of the function, just before closing the handles (note the empty error conditions that you need to fill in):

C++ code listing for sample (Copy Code):

fprintf (stderr, "\n");
char szCookieBuf[512];
DWORD ccCookieBufSize=512;
DWORD dwErr=0;

if (!InternetGetCookieEx("http://jsanders4/","MyHttpCookie",szCookieBuf,&ccCookieBufSize,0,NULL))
{

    dwErr=GetLastError();
   
switch (dwErr)
    {

        case ERROR_INSUFFICIENT_BUFFER:
            break;

        case ERROR_NO_MORE_ITEMS:
           
break;

        default:
           
break;

    };

}
else
{
    fprintf (stderr,
"Cookie found: %s\n", szCookieBuf);
}

ccCookieBufSize=512;

if (!InternetGetCookieEx("http://jsanders4/","MyHttpOnlyCookie",szCookieBuf,&ccCookieBufSize,INTERNET_COOKIE_HTTPONLY,NULL))
{

    dwErr=GetLastError();
    switch (dwErr)
    {

        case ERROR_INSUFFICIENT_BUFFER:
            break;

        case ERROR_NO_MORE_ITEMS:
           
break;

        default:
           
break;

    };

}
else
{
    fprintf (stderr,
"Cookie found: %s\n", szCookieBuf);
}

I put the page to write the cookies on one of my servers and pointed the httpauth.exe to that page.  This code works fine and does read the HttpOnly cookie.  Try and remove the flag and you will see the call fail!

Let me know if this was a help to you!

Published Thursday, June 04, 2009 11:56 AM by jpsanders
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker