共用方式為


CreatingCookieEventArgs.IsPersistent 屬性

定義

取得值,指出驗證 Cookie 是否應保留至目前工作階段以後。

public:
 property bool IsPersistent { bool get(); };
public bool IsPersistent { get; }
member this.IsPersistent : bool
Public ReadOnly Property IsPersistent As Boolean

屬性值

如果 Cookie 應保留至目前工作階段以後,則為 true,否則為 false

範例

下列範例顯示 事件的事件處理常式 CreatingCookie 。 處理常式會從 CreatingCookieEventArgs 物件擷取使用者值,以自訂驗證 Cookie。 物件的 IsPersistent 屬性 FormsAuthenticationTicket 會設定為 屬性中的 IsPersistent 值。

void AuthenticationService_CreatingCookie(object sender, 
    System.Web.ApplicationServices.CreatingCookieEventArgs e)
{
    FormsAuthenticationTicket ticket = new
          FormsAuthenticationTicket
            (1,
             e.UserName,
             DateTime.Now,
             DateTime.Now.AddMinutes(30),
             e.IsPersistent,
             e.CustomCredential,
             FormsAuthentication.FormsCookiePath);

    string encryptedTicket =
         FormsAuthentication.Encrypt(ticket);

    HttpCookie cookie = new HttpCookie
         (FormsAuthentication.FormsCookieName,
          encryptedTicket);
    cookie.Expires = DateTime.Now.AddMinutes(30);

    HttpContext.Current.Response.Cookies.Add(cookie);
    e.CookieIsSet = true;
}
Sub AuthenticationService_CreatingCookie(ByVal sender As Object, _
                 ByVal e As System.Web.ApplicationServices.CreatingCookieEventArgs)
    Dim ticket As FormsAuthenticationTicket = New _
       FormsAuthenticationTicket _
        (1, _
         e.Username, _
         DateTime.Now, _
         DateTime.Now.AddMinutes(30), _
         e.IsPersistent, _
         e.CustomCredential, _
         FormsAuthentication.FormsCookiePath)
        
    Dim encryptedTicket As String = FormsAuthentication.Encrypt(ticket)
    
    Dim cookie As HttpCookie = New _
        HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
    cookie.Expires = DateTime.Now.AddMinutes(30)
    
    HttpContext.Current.Response.Cookies.Add(cookie)
    e.CookieIsSet = True
End Sub

備註

當您建立 FormsAuthenticationTicket 物件時,您可以使用 IsPersistent 屬性來指定驗證 Cookie 是否保留超過目前的會話。

適用於