2010-05-18 31 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