HtmlPage.Document.Cookies empty
Firefox shows that there are 3 (non-expired) cookies and I am able to access them in regular ASP.NET aspx.cs code behind. I also have a Silverlight user control on the same page, but when I attempt to access the same cookie it is unable to find any. HtmlPage.Document.Cookies
count is 0.
What could I be doing wrong?
I'm using this code:
private string GetCookie(string key)
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
foreach (string cookie in cookies)
{
string[] keyValue = cookie.Split('=');
if (keyValue.Length == 2)
{
if (keyValue[0] == key)
return keyValue[1];
}
}
return null;
}
from here
I'm calling it from my view model开发者_开发百科:
public AQViewModel()
{
context = new AQContext();
string cookie = GetCookie("MyCookie");
.....
}
If those cookies are HttpOnly cookies (i.e. containing the HttpOnly
flag when created) you won't be able to access them in client scripts such as javascript and Silverlight. For example that's the case with session and forms authentication cookies in ASP.NET.
A valid solution is to read the cookie from within a WCF service (in my case RIA services) and return it to the Silverlight app
- Add
System.Web
reference to where your wcf classes are var name = HttpContext.Current.Request.Cookies.Get(cookieName);
this will work for httpOnly, secure, and regular cookies too.
精彩评论