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 是否保留到当前会话之外。

适用于