CookieContainer bug?

Posted by Salar on Stack Overflow See other posts from Stack Overflow or by Salar
Published on 2009-06-26T06:41:41Z Indexed on 2010/03/14 10:55 UTC
Read the original article Hit count: 195

Filed under:
|
|

I'm confused how CookieContainer handles domain, so I create this test. This test shows cookieContainer doesn't return any cookie for "site.com" but according to RFC it should return at least 2 cookies.

Isn't it a bug?

How make it to work?

Here is a discussion about this bug:

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c4edc965-2dc2-4724-8f08-68815cf1dce6

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

<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    CookieContainer getContainer()
    {
    	CookieContainer result = new CookieContainer();

    	Uri uri = new Uri("http://sub.site.com");
    	string cookieH = @"Test1=val; domain=sub.site.com; path=/";
    	result.SetCookies(uri, cookieH);

    	cookieH = @"Test2=val; domain=.site.com; path=/";
    	result.SetCookies(uri, cookieH);

    	cookieH = @"Test3=val; domain=site.com; path=/";
    	result.SetCookies(uri, cookieH);

    	return result;
    }

    void Test()
    {
    	CookieContainer cookie = getContainer();
    	lblResult.Text += "<br>Total cookies count: " + cookie.Count + " &nbsp;&nbsp; expected: 3";

    	Uri uri = new Uri("http://sub.site.com");
    	CookieCollection coll = cookie.GetCookies(uri);
    	lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " &nbsp;&nbsp; expected: 2";

    	uri = new Uri("http://other.site.com");
    	coll = cookie.GetCookies(uri);
    	lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " &nbsp;&nbsp; expected: 2";

    	uri = new Uri("http://site.com");
    	coll = cookie.GetCookies(uri);
    	lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " &nbsp;&nbsp; expected: 2";

    }

    protected void Page_Load(object sender, EventArgs e)
    {
    	Test();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>CookieContainer Test Page</title>
</head>
<body>
    <form id="frmTest" runat="server">
    <asp:Label ID="lblResult" EnableViewState="false" runat="server"></asp:Label>
    </form>
</body>
</html>

© Stack Overflow or respective owner

Related posts about cookiecontainer

Related posts about c#