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

備註

類別 AuthenticationServiceCookieIsSet 檢查 屬性,以判斷是否已建立驗證 Cookie。 如果您在事件的事件處理常式中建立驗證 Cookie, CreatingCookie 請將此值 true 設定為 。 如果 CookieIsSet 設定為 false (預設值) ,類別會 AuthenticationService 建立驗證 Cookie,這會覆寫您在事件處理常式 CreatingCookie 中建立的任何 Cookie。

適用於

另請參閱