从应用程序获取所有cookie [英] Get all cookies from application
问题描述
hii,
我想从我的c#.net应用程序中获取所有cookie的列表.
所以如何获取呢?
hii,
i want list of all the cookies from my c#.net application.
so how can i get it?
推荐答案
尝试一下:
Try this:
<%@ 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>
参考号:显示所有Cookie(C#) [
Ref.:Display all Cookie (C#)[^]
好吧.HttpRequest.Cookies是一个集合.所以使用LINQ:
Well.. HttpRequest.Cookies is a collection. So use LINQ:
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];
}
底线:您无法避免遍历整个Cookie集合.但是您可以使用LINQ轻松实现.
或
Bottom line: you can''t get away from iterating over the entire cookie collection. But you can do it easily using LINQ.
Or
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:
string[] cookies = Request.Cookies.AllKeys;
foreach (string cookie in cookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
无论是否是页面,它都应获取当前设置的所有cookie.您可能需要检查一下为cookie设置了什么路径.可能是您创建了仅限于页面或目录的cookie.在创建cookie时将路径设置为/,应该可以看到该域名的所有cookie.
--Amit
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
这篇关于从应用程序获取所有cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!