How to Access a Cookie Created in Silverlight from an Asp.net page [Resolved]

Posted by Vuyiswamb under Regular Expressions on 5/25/2011 | Points: 10 | Views : 3716 | Status : [Member] [MVP] [Administrator] | Replies : 2
Good Day All

I have another question. I have a Silverlight application and in one of the Silverlight pages i am hosting an Asp.net Page using a RadhtmlContainer(Telerik). I created a Cookie in Silverlight like this

public 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);
}


and i have the same generic function to access 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;
}


so i am trying to access this cookie in an asp.net page that is hosted on the the html Container , but i dont find the cookie.

Basically what i want to Achieve is

i want to access a value that is being created in Silverlight e.g "userid" "Username" normally i store it in the Cookie , so now i want to do a database insert , i need to do that insert from that asp.net page, but i cant get hold of that cookie value.

Thanks

Thank you for posting at Dotnetfunda
[Administrator]



Responses

Posted by: Rajesh_a_kumar on: 7/5/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Vuyiswa,

I don't think we will be able to access client side cookies from server side (not 100% sure). However, we have a work around. As Cookies are available at client side and is the same with Silverlight XAP file, SL is able to Set and Get cookies. So, immediate solution can be as follows:

1. Create a Javascript function that has access to Cookies.
2. Register the javascript in code behind's Page_Load event.
3. Pass the key as parameter.
4. Let the Javascript to read the cookies, extract them and return the actual value.
5. Javascript can either return the value or set the value to one of the ASP.Net or Html control available on aspx page.
6. Read the value from that control in code behind page.

Ex:
Javascript code can be:
<script type="text/javascript">

function display(key_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == key_name) {
document.getElementById("ShowValueButton1").value = y;
}
}
}
</script>


HTML code can be:
<body >

<form id="form1" runat="server">

<asp:TextBox runat="server" ID="ASPTextBoxKey"></asp:TextBox>
<input id="ShowValueButton1" type="button" value="button" runat="server"/>
<asp:Button runat="server" ID="GetCookieValue" onclick="GetCookieValue_Click" />
</body>


Code behind page can be:
protected void Page_Load(object sender, EventArgs e)

{
GetCookieValue.Attributes.Add("onclick", " return display('" + ASPTextBoxKey.Text +"');");
}


 protected void GetCookieValue_Click(object sender, EventArgs e)

{

}


Please let me know if you have questions.

Regards,
Rajesh

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/5/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Thank you Rajesh your valuable answer.


Thank you for posting at Dotnetfunda
[Administrator]

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response