views:

32

answers:

3

i want to add 3 serial key to client system using httpcookie when he visited my website my website off course in asp.net MVC

but

serial key is different different not same.

when i add a 4th cookie then 1 key is automatically deleted.

how i can do this.

when user want to see then he can see recent 3 key.

Are you know how to add this cookie to client system by asp.net mvc website.

how i can add key1 , key 2 , key3 to client system.

A: 

You can add cookies to the user when a user starts a session. This can be done by handling the session start even in the global.asax file of your application the adding in the logic code for creating these keys.

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

Then for the 4th cookie thing your were talking about you can create your code logic by referencing the users cookies by using Response.Cookies["cookie"] or remove one using Request.Cookies.Remove("cookie");

James Santiago
A: 

All browser limit in one way or another the amount of cookies a certain website can store. If for example your browser accepts n cookies, the when you send the n+1 the browser will delete your oldest one. It seems to me that this is what is going on.

A possible solution is to use one cookie with various subvalues instead of a single one. That way you can have in one cookie as many values as you want (subjected always to the cookie maximum size limit which is 4086 bytes).

The code to do that would be something like:

//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");
}
Kiranu
+1  A: 

Here's how you can do that.

Writing the serial-keys.

//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);

Reading the serial-key cookie.

//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.
}
this. __curious_geek