كيفية إضافة المفتاح التسلسلي إلى نظام العميل عندما يزور موقعنا على الويب

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

  •  02-10-2019
  •  | 
  •  

سؤال

أرغب في إضافة 3 مفتاح تسلسلي إلى نظام العميل باستخدام httpcookie عندما زار موقع الويب الخاص بي موقع الويب الخاص بي خارج الدورة التدريبية في ASP.NET MVC

لكن

المفتاح التسلسلي مختلف ليس نفسه.

عندما أضيف ملف تعريف الارتباط الرابع ، يتم حذف مفتاح واحد تلقائيًا.

كيف يمكنني فعل هذا.

عندما يريد المستخدم أن يرى ثم يمكنه رؤية مفتاح 3 حديث.

هل تعرف كيفية إضافة ملف تعريف الارتباط هذا إلى نظام العميل بواسطة موقع ASP.NET MVC.

كيف يمكنني إضافة key1 ، المفتاح 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.
}

نصائح أخرى

يمكنك إضافة ملفات تعريف الارتباط إلى المستخدم عندما يبدأ المستخدم جلسة. يمكن القيام بذلك عن طريق معالجة بدء الجلسة حتى في ملف Global.asax لتطبيقك الإضافة في رمز المنطق لإنشاء هذه المفاتيح.

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

ثم بالنسبة لشيء ملف تعريف الارتباط الرابع الذي كنت تتحدث عنه ، يمكنك إنشاء منطق الرمز الخاص بك من خلال الرجوع إلى ملفات تعريف الارتباط للمستخدمين باستخدام Response.Cookies["cookie"] أو إزالة واحدة باستخدام Request.Cookies.Remove("cookie");

تحد جميع المتصفح بطريقة أو بأخرى كمية ملفات تعريف الارتباط التي يمكن لموقعها الإلكتروني تخزينها. على سبيل المثال ، إذا قبلت المستعرض الخاص بك ملفات تعريف الارتباط ، عند إرسال N+1 ، سيحذف المتصفح أقدمه. يبدو لي أن هذا ما يجري.

الحل المحتمل هو استخدام ملف تعريف ارتباط واحد مع مختلف القيم الفرعية بدلاً من واحد. وبهذه الطريقة ، يمكنك الحصول على ملف تعريف ارتباط واحد كما تريد (تخضع دائمًا لحد أقصى حجم لحجم ملفات تعريف الارتباط وهو 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");
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top