Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii,

i want list of all the cookies from my c#.net application.
so how can i get it?
Posted

Try this:
ASP.NET
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    void Page_Load()
    {
        ArrayList colCookies = new ArrayList();
        for (int i = 0; i < Request.Cookies.Count; i++)
            colCookies.Add(Request.Cookies[i]);

        grdCookies.DataSource = colCookies;
        grdCookies.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Get All Cookies</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:GridView
        id="grdCookies"
        Runat="server"/>
        
    </div>
    </form>
</body>
</html>

Ref.:Display all Cookie (C#)[^]
 
Share this answer
 
v2
Comments
_Amy 25-Jul-12 6:31am    
Is it used to list of all the cookies from an application?
Prasad_Kulkarni 25-Jul-12 6:33am    
Exactly Amit, and here is question
i want list of all the cookies from my c#.net application.
Ankur Ramanuj 25-Jul-12 6:34am    
no its not working..its display only current browser's cookie..
Ankur Ramanuj 25-Jul-12 6:43am    
guys tell me how can i get all the cookie from all browser..
Well.. HttpRequest.Cookies is a collection. So use LINQ:

C#
var qry = from cookieName in Request.Cookies.Keys
          where cookieName.StartsWith("google")
          select cookieName;

foreach(var item in qry)
{
   // get the cookie and deal with it.
   var cookie = Request.Cookies[item];
}

Bottom line: you can't get away from iterating over the entire cookie collection. But you can do it easily using LINQ.

Or
C#
void Page_Load()
    {

        ArrayList colCookies = new ArrayList();
        for (int i = 0; i < Request.Cookies.Count; i++)
            colCookies.Add(Request.Cookies[i]);

        grdCookies.DataSource = colCookies;
        grdCookies.DataBind();
    }


View this[^] link for more information.

Removing cookie from your entire application try this:
C#
string[] cookies = Request.Cookies.AllKeys;
foreach (string cookie in cookies)
{
            Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}

It should get all the cookies that are currently set whether it's the page or not. You may want to check to see what the Path is set for the cookies. It could be that you have created cookies that are restricted to a page or directory. Setting the path to / when creating a cookie should allow all cookies for that domain name to be visible.

--Amit
 
Share this answer
 
v2
Comments
Ankur Ramanuj 25-Jul-12 6:32am    
i had try this but its display only one browser's cookies but i want all the cookies list in my application actually ruaning
_Amy 25-Jul-12 6:44am    
Check my updated answer..
_Amy 25-Jul-12 6:55am    
Is it working now?
StianSandberg 25-Jul-12 7:21am    
It is not possible to get cookies from other browsers than the browser browsing your site.
_Amy 25-Jul-12 7:24am    
"Request.Cookies.AllKeys" will return all the cookies created by your application in a browser(single browser).

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