Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Silverlight 2 -- Set Cookie RSS

9 replies

Last post Dec 25, 2008 09:44 PM by bezobav

(0)
  • thenewsequoyan

    thenewsequoyan

    Member

    77 Points

    31 Posts

    Silverlight 2 -- Set Cookie

    Mar 18, 2008 12:29 PM | LINK

    Hi All,

    How do you set a browser cookie for the page hosting a SL2 app from within that app?

    I've read all the documentation and posts on this that I could find and have not found an answer to what should be a simple question...

    Thanks for any help.

    _T

  • Anand Tripathi

    Anand Tripathi

    Member

    73 Points

    50 Posts

    Re: Silverlight 2 -- Set Cookie

    Mar 18, 2008 12:39 PM | LINK

    You can call a Asp.Net server side function with the help of javascript function to save a cookie.

    http://www.anandtripathi.googlepages.com/
  • thenewsequoyan

    thenewsequoyan

    Member

    77 Points

    31 Posts

    Re: Silverlight 2 -- Set Cookie

    Mar 18, 2008 06:20 PM | LINK

    Thanks for the reply. Any sample code for this?

     All I'm trying to do is set a cookie in SL2 that the aspx code-behind from another page can check.

     _T

  • jackbond

    jackbond

    Contributor

    5812 Points

    1559 Posts

    Re: Silverlight 2 -- Set Cookie

    Mar 18, 2008 08:38 PM | LINK

    thenewsequoyan

    Thanks for the reply. Any sample code for this?

    How about this? :)

    public class CookieManager

    {

    public static void SetCookie(string key, string val, TimeSpan? expires)

    {

    SetCookie(key, val, expires, null, null, false);

    }

    public static void SetCookie(string key, string val, TimeSpan? expires, string path, string domain, bool secure)

    {

    StringBuilder fullCookie = new StringBuilder();

    fullCookie.Append(string.Concat(key, "=", val));

    if(expires.HasValue)

    {

    DateTime expire = DateTime.UtcNow + expires.Value;fullCookie.Append(string.Concat(";expires=", expire.ToString("R")));

    }

    if (path != null)

    {

    fullCookie.Append(string.Concat(";path=", path));

    }

    if (domain != null)

    {

    fullCookie.Append(string.Concat(";domain=", domain));

    }

    if (secure)

    {

    fullCookie.Append(";secure");

    }

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

    }

    }

  • jeetumaker

    jeetumaker

    Member

    525 Points

    92 Posts

    Re: Re: Silverlight 2 -- Set Cookie

    Mar 18, 2008 09:21 PM | LINK

    You can also explore the option of IsolatedStorage (IS) in Silverlight.  IS is something like "cookies on steriods" where you get more flexibility than the traditional cookie and storate limie is 1MB per Silverlight app which could be extended by taking permission from the user.  The main advantage of IS is you could store any object/file streams and only your app gets access to it from alls browser (IE & FireFox) installed on the same machine.

    I hope this helps.  If so Please click on the "Mark As Answer" .

    I hope this helps. Please click on the "Mark as Answer" if it has answered your question.

    Thanks
    Jeetu Maker
    http://jeetumaker.blogspot.com
    (Light_up_your_apps) using Silverlight.2.0;
  • thenewsequoyan

    thenewsequoyan

    Member

    77 Points

    31 Posts

    Re: Silverlight 2 -- Set Cookie

    Mar 19, 2008 12:53 PM | LINK

    Mr. Bond,

    Thank you very much! That's done it.

    Here's a VB.Net version for anyone interested. I've added get & delete methods. Additionally I had to do a little investigation to get cookie debugging working on localhost. It wasn't hard, just needed to pass an empty string to Path for the SetCookie method.

    Imports System.Text

    Imports System.Windows.Browser

    Public Class CookieManager

    Public Shared Sub SetCookie(ByVal key As String, ByVal val As String, ByVal expires As TimeSpan)

    SetCookie(key, val, expires, Nothing, Nothing, False)

    End Sub

    Public Shared Sub SetCookie(ByVal key As String, ByVal val As String, ByVal expires As System.Nullable(Of TimeSpan), ByVal path As String, ByVal domain As String, ByVal secure As Boolean)

    Try

    Dim fullCookie As New StringBuilder() fullCookie.Append(String.Concat(key, "=", val))

    If expires.HasValue Then

    Dim expire As DateTime = DateTime.UtcNow + expires.Value fullCookie.Append(String.Concat(";expires=", expire.ToString("R")))

    End If

    If path IsNot Nothing Then

    fullCookie.Append(String.Concat(";path=", path))

    End If

    If domain IsNot Nothing Then

    fullCookie.Append(String.Concat(";domain=", domain))

    End If

    If secure Then

    fullCookie.Append(";secure")

    End If

    HtmlPage.Document.SetProperty("cookie", fullCookie.ToString())

    Catch ex As Exception

    Throw New Exception("Problem with SetCookie(). Error: " & ex.Message, ex)

    End Try

    End Sub

    Public Shared Function GetCookieValue(ByVal Key As String) As String

    Try

    Dim cookies() As String = Split(HtmlPage.Document.Cookies, "; ") For Each c As String In cookies

    If InStr(c.ToLower, Key.ToLower) Then

    Dim s() As String = Split(c, "=") Return s(1)

    End If

    Next

    Return ""

    Catch ex As Exception

    Throw New Exception("Problem with GetCookieValue(). Error: " & ex.Message, ex)

    End Try

    End Function

    Public Shared Sub DeleteCookie(ByVal key As String, ByVal path As String)

    Try

    Dim fullCookie As New StringBuilder()

    fullCookie.Append(String.Concat(key, "="))

    Dim expire As DateTime = DateTime.UtcNow.Subtract(New TimeSpan(1000000000))

    fullCookie.Append(String.Concat(";expires=", expire.ToString("R")))

    fullCookie.Append(String.Concat(";path=", path))

    HtmlPage.Document.SetProperty("cookie", fullCookie.ToString())

    Catch ex As Exception Throw New Exception("Problem with DeleteCookie(). Error: " & ex.Message, ex)

    End Try

    End Sub

    End Class

  • thenewsequoyan

    thenewsequoyan

    Member

    77 Points

    31 Posts

    Re: Re: Silverlight 2 -- Set Cookie

    Mar 19, 2008 12:55 PM | LINK

    Jeetu,

    Thanks for the post.

    If you read the scenario in my first post of this thread you'll see that I need to check the cookie in aspx code behind (ie no silverlight) so IS is not an option.

    _T

  • laurent_31

    laurent_31

    Member

    4 Points

    2 Posts

    Re: Silverlight 2 -- Set Cookie

    Jul 04, 2008 07:10 AM | LINK

    I repply my question in this post because it's allmost the same.

    http://silverlight.net/forums/p/12892/67821.aspx#67821

    How to get in a ASP.NET page a cookies who was send by the silverlight application?

    Thanks to you,

  • Sergey.Lutay

    Sergey.Lutay

    Star

    13559 Points

    2381 Posts

    Re: Silverlight 2 -- Set Cookie

    Jul 16, 2008 07:52 AM | LINK

    Hi,

    Look here.

    Silverlight 2

    (If I answered the question, please click on "mark as answer".)

    Twitter

    Sincerely,
    Sergii Lutai

    MCT, MCPD, Head of Dev. Dept at DCT
  • bezobav

    bezobav

    Member

    5 Points

    4 Posts

    Re: Silverlight 2 -- Set Cookie

    Dec 25, 2008 09:44 PM | LINK

    DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(250); 

    string cookie = string.Format("key=value;expires={0}", expiration.ToString("R"));

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

    // string cookie = HtmlPage.Document.GetProperty("cookie");