search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

View Other Ask Dr. Dotnetsky Posts   Ask New Question 
Cookies problem in silverlight 2.0
divya rocks posted at Tuesday, April 08, 2008 6:42 AM

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 at Tuesday, April 08, 2008 7: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
Sanjay Verma replied to divya rocks at Tuesday, April 08, 2008 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 at Tuesday, May 13, 2008 3: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 at Tuesday, September 22, 2009 3:17 AM
end of post