Domanda

voglio aggiungere 3 chiave seriale al sistema client utilizzando HttpCookie quando ha visitato il mio sito web il mio sito web fuori rotta in asp.net MVC

ma

codice seriale è diversa diverso non è lo stesso.

quando aggiungo un 4 ° biscotto poi 1 chiave viene eliminata automaticamente.

Come posso fare questo.

quando l'utente vuole vedere poi si può vedere recente tasto 3.

Sei sapere come aggiungere questo cookie al sistema client da asp.net MVC sito web.

Come si aggiunge key1, il tasto 2, key3 al sistema client.

È stato utile?

Soluzione

Ecco come si può fare.

La scrittura dei-codici seriali.

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

Leggendo il cookie di serie-chiave.

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

Altri suggerimenti

È possibile aggiungere i cookie per l'utente quando un utente inizia una sessione. Questo può essere fatto gestendo la sessione di avviare anche nel file Global.asax dell'applicazione l'aggiunta nel codice logica per la creazione di questi tasti.

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

Poi per la cosa biscotto quarto tua parlavano è possibile creare la tua logica del codice facendo riferimento alle cookie degli utenti utilizzando Response.Cookies["cookie"] o rimuovere uno utilizzando Request.Cookies.Remove("cookie");

Tutti limite del browser in un modo o nell'altro la quantità di biscotti un certo sito web in grado di memorizzare. Se per esempio il browser accetta i cookie n, il quando si invia il n + 1 il browser cancellerà il vostro uno più vecchio. Mi sembra che questo è quello che sta succedendo.

Una possibile soluzione è quella di utilizzare un biscotto con varie subvalues ??invece di uno solo. In questo modo si può avere in un unico cookie di tanti valori che si desidera (soggetto sempre al limite di dimensione massima di cookie, che è 4086 byte).

Il codice per fare questo sarebbe qualcosa di simile:

//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");
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top