Uređivanje

Dijeli putem


CreatingCookieEventArgs.IsPersistent Property

Definition

Gets a value that indicates whether the authentication cookie should be retained beyond the current session.

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

Property Value

true if the cookie should be retained beyond the current session; otherwise, false.

Examples

The following example shows an event handler for the CreatingCookie event. The handler retrieves user values from the CreatingCookieEventArgs object in order to customize the authentication cookie. The IsPersistent property of the FormsAuthenticationTicket object is set to the value in the IsPersistent property.

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

Remarks

When you create a FormsAuthenticationTicket object, you can use the IsPersistent property to specify whether the authentication cookie is retained beyond the current session.

Applies to