Sono confuso su come CookieContainer gestisce il dominio, quindi creo questo test. Questo test mostra che cookieContainer non restituisce alcun cookie per "esempio.com" ma secondo RFC dovrebbe restituire almeno 2 cookie.Bug CookieContainer?
Non è un bug?
Come farlo funzionare?
Ecco una discussione su questo 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.example.com");
string cookieH = @"Test1=val; domain=sub.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test2=val; domain=.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test3=val; domain=example.com; path=/";
result.SetCookies(uri, cookieH);
return result;
}
void Test()
{
CookieContainer cookie = getContainer();
lblResult.Text += "<br>Total cookies count: " + cookie.Count + " expected: 3";
Uri uri = new Uri("http://sub.example.com");
CookieCollection coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://other.example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " 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>
Ho provato anche questo molte volte prima. Ho finito per leggere io stesso l'intestazione del cookie e memorizzarlo da qualche altra parte. – Nippysaurus
Devo usare CookieContainer perché è l'unico modo per inviare cookie a HttpWebRequest. – Salar
Non posso credere che finalmente ho avuto uno scenario in cui cambiare il framework da 4.0 a 3.5 (non stavo usando 4.0 roba) ha rotto il mio programma.Mi ci è voluto un po 'di tempo per capire perché mancavano improvvisamente i cookie di sessione per l'autenticazione. Hanno risolto questo problema in 4.0, quindi cambiare il framework ha introdotto un bug nel mio programma :-) – VVS