Asp.net mvc 当他访问我们的网站时,如何向客户端系统添加序列密钥

Asp.net mvc 当他访问我们的网站时,如何向客户端系统添加序列密钥,asp.net-mvc,system,httpcookie,Asp.net Mvc,System,Httpcookie,我想在他访问我的网站时,使用httpcookie向客户端系统添加3个序列密钥 我的网站在asp.net MVC中脱离课程 但是 串行密钥是不同的 当我添加第四个cookie时,会自动删除一个密钥 我怎么能做到这一点 当用户想要查看时,他可以查看最近的3个键 您知道如何通过asp.net mvc网站将此cookie添加到客户端系统吗 如何将密钥1、密钥2、密钥3添加到客户端系统。用户启动会话时,您可以向用户添加cookie。这可以通过在应用程序的global.asax文件中处理会话启动来完成,并添

我想在他访问我的网站时,使用httpcookie向客户端系统添加3个序列密钥 我的网站在asp.net MVC中脱离课程

但是

串行密钥是不同的

当我添加第四个cookie时,会自动删除一个密钥

我怎么能做到这一点

当用户想要查看时,他可以查看最近的3个键

您知道如何通过asp.net mvc网站将此cookie添加到客户端系统吗


如何将密钥1、密钥2、密钥3添加到客户端系统。

用户启动会话时,您可以向用户添加cookie。这可以通过在应用程序的global.asax文件中处理会话启动来完成,并添加用于创建这些键的逻辑代码

void Session_OnStart() {
    HttpCookie key1 = new HttpCookie("key1","123");   
    Request.Cookies.Add(key1);
}

然后,对于您谈论的第四个cookie,您可以通过使用
Response.cookies[“cookie”]
引用用户cookies来创建代码逻辑,或者使用
Request.cookies.remove(“cookie”)删除一个cookie

所有浏览器都以某种方式限制某个网站可以存储的cookie数量。例如,如果您的浏览器接受n个cookie,则当您发送n+1时,浏览器将删除最旧的cookie。在我看来,这就是正在发生的事情

一个可能的解决方案是将一个cookie与不同的子值一起使用,而不是使用单个cookie。这样,您可以在一个cookie中拥有任意多的值(始终受cookie最大大小限制(4086字节)

这样做的代码如下所示:

//This code creates the cookie, ads some subvalues to it and then adds it to the request so its sent to the browser
HttpCookie cookie = new HttpCookie();
cookie.Name = "MyKeys";            
cookie.Values.Add("Key1", "ValueKey1");
cookie.Values.Add("Key2", "ValueKey2");
cookie.Values.Add("Key3", "ValueKey3");
//etc...

Request.Cookies.Add(cookie);

//This code reads the data from the cookie.
HttpCookie rcookie = Response.Cookies.Get("MyKeys");
string value1, value2, value3;
//We verify it the variable is null because the cookie might not exist in the users browser. If so, this variable would be null
if (rcookie != null) 
{
    value1 = rcookie.Values.Get("Key1");
    value2 = rcookie.Values.Get("Key2");
    value3 = rcookie.Values.Get("Key3");
}

这是你可以做到的

写入串行键。

//create a cookie
HttpCookie SerialKeys = new HttpCookie("SerialKeys");

//Add serial-key-values in the cookie
SerialKeys.Values.Add("key1", "your-first-serialkey");
SerialKeys.Values.Add("key2", "your-second-serialkey");
SerialKeys.Values.Add("key3", "your-third-serialkey");
SerialKeys.Values.Add("key4", "your-fourth-serialkey");

//set cookie expiry date-time. Made it to last for next 12 hours.
SerialKeys.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(SerialKeys);
//Assuming user comes back after several hours. several < 12.
//Read the cookie from Request.
HttpCookie SerialKeys = Request.Cookies["SerialKeys"];
if (SerialKeys == null)
{
    //No cookie found or cookie expired.
    //Handle the situation here, Redirect the user or simply return;
}

//ok - cookie is found.
//Gracefully check if the cookie has the key-value as expected.
if (!string.IsNullOrEmpty(SerialKeys.Values["key1"]))
{
    string serialKey = SerialKeys.Values["key1"].ToString();
    //Yes key1 is found. Mission accomplished.
}

if (!string.IsNullOrEmpty(SerialKeys.Values["key2"]))
{
    string serialKey = SerialKeys.Values["key2"].ToString();
    //Yes key2 is found. Mission accomplished.
}
读取串行密钥cookie。

//create a cookie
HttpCookie SerialKeys = new HttpCookie("SerialKeys");

//Add serial-key-values in the cookie
SerialKeys.Values.Add("key1", "your-first-serialkey");
SerialKeys.Values.Add("key2", "your-second-serialkey");
SerialKeys.Values.Add("key3", "your-third-serialkey");
SerialKeys.Values.Add("key4", "your-fourth-serialkey");

//set cookie expiry date-time. Made it to last for next 12 hours.
SerialKeys.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(SerialKeys);
//Assuming user comes back after several hours. several < 12.
//Read the cookie from Request.
HttpCookie SerialKeys = Request.Cookies["SerialKeys"];
if (SerialKeys == null)
{
    //No cookie found or cookie expired.
    //Handle the situation here, Redirect the user or simply return;
}

//ok - cookie is found.
//Gracefully check if the cookie has the key-value as expected.
if (!string.IsNullOrEmpty(SerialKeys.Values["key1"]))
{
    string serialKey = SerialKeys.Values["key1"].ToString();
    //Yes key1 is found. Mission accomplished.
}

if (!string.IsNullOrEmpty(SerialKeys.Values["key2"]))
{
    string serialKey = SerialKeys.Values["key2"].ToString();
    //Yes key2 is found. Mission accomplished.
}
//假设用户几个小时后回来。有好几个<12。
//从请求中读取cookie。
HttpCookie SerialKeys=Request.Cookies[“SerialKeys”];
if(SerialKeys==null)
{
//未找到cookie或cookie已过期。
//在这里处理情况,重定向用户或直接返回;
}
//确定-找到cookie。
//优雅地检查cookie是否具有预期的键值。
如果(!string.IsNullOrEmpty(SerialKeys.Values[“key1”]))
{
字符串serialKey=SerialKeys.Values[“key1”].ToString();
//是,找到键1。任务完成。
}
如果(!string.IsNullOrEmpty(SerialKeys.Values[“key2”]))
{
字符串serialKey=SerialKeys.Values[“key2”].ToString();
//是,找到键2。任务完成。
}