Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Techies,

I have a requirement in my application that i need to read a cookie from the browser which is created by the other application. I am not able to read the cookie value. The cookie is a session cookie which will expires when browser lost the session. I used the below code to read all the cookies in browser. But, i couldn't get the cookie that i am looking for.

Request.ServerVariables["HTTP_COOKIE"];


Please help me in reading the cookie value through .NET application.

Regards,
Vijay Kumar E,
Posted
Updated 22-Jan-12 23:37pm
v2

1 solution

You can get the cookie value using following function.

C#
getCookieValue("Cookie_key");

public static string getCookieValue(string strKey)
    {
        if (HttpContext.Current.Response.Cookies[strKey] != null && HttpContext.Current.Response.Cookies[strKey].Value + "" != "")
        {
            return HttpContext.Current.Response.Cookies[strKey].Value;
        }
        else if (HttpContext.Current.Request.Cookies[strKey] != null)
        {
            HttpContext.Current.Response.Cookies[strKey].Value = HttpContext.Current.Request.Cookies[strKey].Value;
            return HttpContext.Current.Request.Cookies[strKey].Value;
        }
        else
        {
            return "";
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900