CreatingCookieEventArgs.CookieIsSet 属性

定义

获取或设置一个值,该值指示是否已创建身份验证 Cookie。

public:
 property bool CookieIsSet { bool get(); void set(bool value); };
public bool CookieIsSet { get; set; }
member this.CookieIsSet : bool with get, set
Public Property CookieIsSet As Boolean

属性值

如果已创建身份验证 Cookie,则为 true;否则为 false

示例

以下示例显示了 事件的事件处理程序 CreatingCookie 。 处理程序从 CreatingCookieEventArgs 对象中检索用户值以自定义身份验证 Cookie。 创建 CookieIsSet 身份验证票证后, 属性设置为 true

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

注解

AuthenticationService 检查 CookieIsSet 属性以确定是否已创建身份验证 Cookie。 如果在事件的事件处理程序中创建身份验证 Cookie,则可以将CreatingCookie此值设置为 true 。 如果 CookieIsSet 设置为 false (默认值) ,则 AuthenticationService 类将创建身份验证 Cookie,这将覆盖在事件处理程序 CreatingCookie 中创建的任何 Cookie。

适用于

另请参阅