2010-05-18 12 views

risposta

6

SNIPPET:

 switch Role: 
    Case A: VARIABLE X = Y; BREAK; 
    CASE B: VARIABLE X = Y2; BREAK; 
    .. 

    End switch 

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
     1, // Ticket version 
     Username.Value, // Username associated with ticket 
     DateTime.Now, // Date/time issued 
     DateTime.Now.AddMinutes(VARIABLE X), // Date/time to expire 
     true, // "true" for a persistent user cookie 
     reader.GetString(0), // User-data, in this case the roles 
     FormsAuthentication.FormsCookiePath);// Path cookie valid for 

    // Encrypt the cookie using the machine key for secure transport 
    string hash = FormsAuthentication.Encrypt(ticket); 
    HttpCookie cookie = new HttpCookie(
     FormsAuthentication.FormsCookieName, // Name of auth cookie 
     hash); // Hashed ticket 

    // Set the cookie's expiration time to the tickets expiration time 
    if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; 

    Response.Cookies.Add(cookie); 
+0

Molto chiaro e utile! Grazie! Se utilizzo il sovraccarico di FormsAuthenticationTicket ricevendo solo username stringa, bool IsPersitent e int timeout, dovrei fare anche la crittografia e l'assegnazione ai cookie? –

+0

Capisco da qui: https://msdn.microsoft.com/en-us/library/w04e17xz(v=vs.100).aspx nelle osservazioni, che FormsCookiePath è impostato automaticamente e quindi la crittografia ecc. fatto lo stesso –

Problemi correlati