Cookies problem in silverlight 2.0

Asked By divya rocks
08-Apr-08 06:42 AM
Earn up to 0 extra points for answering this tough question.

hi.

how to remove cookies in silverlight application...!!

i have on login page(aspx) in  taht button click  i create cookies .

before that i  write code in pageload for deleting  cookies  like

HttpCookie hc = new HttpCookie("");

button click

HttpCookie hc = new HttpCookie(userdetails[0], userdetails[1]);

hc.Expires = DateTime.MinValue;

Response.Cookies.Add(hc);

 

i call this cookie in xaml page(silverlight 2.0)

 

using System.Windows.Browser;

  string  str = string.Empty;

str = HtmlPage.Document.Cookies;

Empid = str.Split('=')[1];

 

}

private static string _empid = string.Empty;

public static string Empid

{

get { return UserDetails._empid; }

set { UserDetails._empid = value; }

}

. here my problem  is  i  have logout button in xaml page.. when  i click it i redirected to login page..., again  i login with another username and password..,  in  string str, it showing old values and new values... how to delete cookies when i log out..

thank u

 

  Cookie

Aravind Kumar replied to divya rocks
08-Apr-08 07:13 AM

This statement HttpCookie hc = new HttpCookie("") will never delete cookies, rather it'll just create a persistant cookie (and deletes a cookie if the client system has reached max cookie limits). Its always advisable to create a cookie with a name. So after creating it, give a expiry minvalue as you do.

Finally, when you logout of the application, this statement will surely remove your cookie from the client - Response.Cookies.Remove("CookieName");

Hope this helps.

  Delete Cookie

Shailendrasinh Parmar replied to divya rocks
08-Apr-08 11:42 PM

Write the following statement on your logout button click event ::

Response.Cookies.Remove("hc");
Response.Redirec("Your Login Page");

Hope this helps.....

you can refer this link for your knowledge...

http://forums.asp.net/t/1227365.aspx

  retrieve and delete cookies within a Silverlight application.

Rakesh Vikram replied to divya rocks
13-May-08 03:11 AM

Try to implement this logic within your application. The below code retrives an existing  cookie and deletes the specified cookie.

---------------------------------------------------------------------------

/// <summary>
/// sets a persistent cookie with huge expiration date
/// </summary>
/// <param name="key">the cookie key</param>
/// <param name="value">the cookie value</param>
private static void SetCookie(string key, string value)
{
    string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;
    DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(2000);
    string cookie = String.Format("{0}={1};expires={2}", key, value, expiration.ToString("R"));
    HtmlPage.Document.SetProperty("cookie", cookie);
}

/// <summary>
/// Retrieves an existing cookie
/// </summary>
/// <param name="key">cookie key</param>
/// <returns>null if the cookie does not exist, otherwise the cookie value</returns>
private 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;
}

/// <summary>
/// Deletes a specified cookie by setting its value to empty and expiration to -1 days
/// </summary>
/// <param name="key">the cookie key to delete</param>
private static void DeleteCookie(string key)
{
    string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;
    DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);
    string cookie = String.Format("{0}=;expires={1}", key, expiration.ToString("R"));
    HtmlPage.Document.SetProperty("cookie", cookie);
}

---------------------------------------------------------------------------

For more information, on cookies properties:

http://msdn2.microsoft.com/en-us/library/ms533693(VS.85).aspx

Hope this helps in provinding a solution.

Rakesh Vikram

 

 

 

  Silverlight
wang mingjun replied to divya rocks
22-Sep-09 03:17 AM
end of post
Create New Account