Search code examples
asp.net-mvcsystemhttpcookie

how to add serial key to client system when he visit our website


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.


Solution

  • 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.
    }