Tuesday, December 6, 2011

How to update a cookie in asp.net C#?

In my previous post we learn how to retrieve cookie in silverlight, now i am showing how to update cookies value in asp.net C# :) why C# ? hahaha because i love this :)

Here is the code for update cookie
string IpAddress="127.0.0.1";
 HttpCookie ck = Request.Cookies["clientIPAdd"];
                if (ck.Value == "undefined" || string.IsNullOrEmpty(ck.Value))
                {
                    ck.Value = IpAddress;
                    Response.Cookies.Set(ck);
                }

I think you remember about my post Cookie , in this post the cookie created by javascript so may be sometims its returns undefined. So when ever it returns undefined or empty value the above code will update the related (clientIPAdd) cookie value to "127.0.0.1"

Cheers :)


No comments:

Post a Comment