Good Evening All

i have another Question. i have created a Function that creates a Cookie with a Domain like this

private static void SetCookieExtedend(String key, String value, String path)
        {

            DateTime expires = DateTime.UtcNow + TimeSpan.FromDays(2000);

            bool secure = false;

            String domain = "Ecash";

            StringBuilder cookie = new StringBuilder();

            cookie.Append(String.Concat(key, "=", value));

            if (expires != null)
            {

                cookie.Append(String.Concat(";expires=", expires.ToString("R")));
            }

            if (!String.IsNullOrEmpty(path))
            {
                cookie.Append(String.Concat(";path=", path));
            }

            if (!String.IsNullOrEmpty(domain))
            {
                cookie.Append(String.Concat(";domain=", domain));
            }

            if (secure)
            {
                cookie.Append(";secure");
            }

            HtmlPage.Document.SetProperty("cookie", cookie.ToString());
        }

and its working well , i have set the Cookie and it was stored as this

UserID=142;expires=Mon, 14 Nov 2016 16:58:19 GMT;domain=Ecash

now i want to retrieve the Value of the Cookie and i have a function that does it like this

public static string GetCookie(string key)
        {
            string[] cookies = HtmlPage.Document.Cookies.Split(';');
            key += '=';
            foreach (string cookie in cookies)
            {
                string cookieStr = cookie.Trim();
                if (cookieStr.StartsWith(key, StringComparison.OrdinalIgnoreCase))
                {
                    string[] vals = cookieStr.Split('=');

                    if (vals.Length >= 2)
                    {
                        return vals[1];
                    }

                    return string.Empty;
                }
            }

            return null;
        }

the Following Line comes back with an empty String

string[] cookies = HtmlPage.Document.Cookies.Split(';');

Why ?

Thanks

Recommended Answers

All 4 Replies

Look into HTTPOnly cookies. I think from what I remembered in asp.net that might lead you to your solution. :)

I normally use:

If Not Request.Cookies("userName") Is Nothing Then
Dim aCookie As HttpCookie = Request.Cookies("userName")
Label1.Text = Server.HtmlEncode(aCookie.Value)
End

Request.Cookies contains a list of cookies available under that domain

use Request.Cokkies("Cookiesname").value for getting values using ASP.Net

Good Day All

I am not creating a Cookie in ASp.net, i am creating the Cookie in Silverlight, the Implementation of this will be different

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.