Page view counter
Silverlight 2 -- Set Cookie
Last post 07-16-2008 3:52 AM by Sergey.Lutay. 8 replies.
Sort Posts:
03-18-2008 8:29 AM
Silverlight 2 -- Set Cookie

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

thenewsequoyan

Loading...
Joined on 02-10-2008
Posts 25
03-18-2008 8:39 AM
Re: Silverlight 2 -- Set Cookie

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

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

Anand Tripathi

Loading...
Joined on 06-21-2007
varanasi India
Posts 38
03-18-2008 2:20 PM
Re: Silverlight 2 -- Set Cookie

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

thenewsequoyan

Loading...
Joined on 02-10-2008
Posts 25
03-18-2008 4:38 PM
Marked as Answer
Re: Silverlight 2 -- Set Cookie

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());

}

}

Jack Bond

Khet - The first Silverlight multiplayer game

Zork I: The Great Underground Empire

jackbond

Loading...
Joined on 03-21-2007
Posts 429
03-18-2008 5:21 PM
Re: Re: Silverlight 2 -- Set Cookie

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;

jeetumaker

Loading...
Joined on 02-22-2008
Los Angeles, California
Posts 34
03-19-2008 8:53 AM
Re: Silverlight 2 -- Set Cookie

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

Loading...
Joined on 02-10-2008
Posts 25
03-19-2008 8:55 AM
Re: Re: Silverlight 2 -- Set Cookie

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

thenewsequoyan

Loading...
Joined on 02-10-2008
Posts 25
07-04-2008 3:10 AM
Re: Silverlight 2 -- Set Cookie

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,

laurent_31

Loading...
Joined on 07-03-2008
Posts 2
07-16-2008 3:52 AM
Re: Silverlight 2 -- Set Cookie

Hi,

Look here.

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)


Sincerely,
Sergey Lutay

Sergey.Lutay

Loading...
Joined on 02-14-2008
Ukraine
Posts 349
Microsoft Communities