Desidero consentire agli amministratori di accedere più a lungo degli utenti normali. Non vedo un gancio per impostare il timeout dei cookie a livello di codice o in un modo basato sui ruoli. Questo è possibile in ASP usando l'autenticazione basata su form?Impostazione della durata del timeout del cookie auth in base al ruolo in ASP.NET
risposta
Sì, puoi farlo. Dovresti generare manualmente il ticket di autenticazione invece di lasciare che il framework lo generi automaticamente.
In base al ruolo utente, la scadenza assegnata al ticket.
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);
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? –
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 –
Grazie! Collegamento perfetto – Wyatt