彼が当社のウェブサイトにアクセスしたときにクライアントシステムにシリアルキーを追加する方法

StackOverflow https://stackoverflow.com/questions/3163336

  •  02-10-2019
  •  | 
  •  

質問

彼が私のウェブサイトにアクセスしたときに、httpcookieを使用してクライアントシステムに3つのシリアルキーを追加したいです。

しかし

シリアルキーは異なる異なり、同じではありません。

4番目のCookieを追加すると、1キーが自動的に削除されます。

これができる方法。

ユーザーが見たいとき、彼は最近の3つのキーを見ることができます。

ASP.NET MVC WebサイトでこのCookieをクライアントシステムに追加する方法を知っていますか?

key1、key 2、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を追加できます。これは、これらのキーを作成するためのロジックコードを追加するアプリケーションのASAXファイルでも、セッション開始を処理することで実行できます。

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

次に、4番目のCookieのことについて、あなたの話をしていたことについては、ユーザーCookieを使用して参照してコードロジックを作成できます Response.Cookies["cookie"] または、使用して削除します Request.Cookies.Remove("cookie");

すべてのブラウザは、特定のWebサイトが保存できるCookieの量を何らかの形で制限します。たとえば、ブラウザがN Cookieを受け入れる場合、N+1を送信すると、ブラウザが最古のものを削除します。これが起こっていることが私には思えます。

可能な解決策は、1つのクッキーを1つではなく、さまざまなサブ値を使用して1つのCookieを使用することです。そうすれば、1つのCookieに必要な数の値を使用できます(常に4086バイトであるCookieの最大サイズの制限に常に供給されます)。

それを行うコードは次のようなものです。

//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");
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top