2010-05-18 54 views

回答

7

是的,你可以做到這一點。您需要手動生成身份驗證票證,而不是讓框架自動生成身份驗證票證。

根據用戶角色,您分配給故障單的失效。

This tutorial show how to generate the ticket manually.

+0

謝謝!完美的聯繫。 – Wyatt 2010-05-18 17:33:04

6

片段:

 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

非常明確,樂於助人!謝謝!如果我使用僅接收字符串用戶名的FormsAuthenticationTicket重載,bool IsPersitent和int timeout,我是否也會執行加密並分配給cookie? – 2016-05-18 08:26:27

+0

我明白從這裏:https://msdn.microsoft.com/en-us/library/w04e17xz(v=vs.100).aspx在備註中,FormsCookiePath是自動設置的,所以加密等將是完成一樣。 – 2016-05-18 08:33:37