共用方式為


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,請將此值 true 設定為 CreatingCookie 。 如果 CookieIsSet 設定為 false (預設值) ,類別 AuthenticationService 會建立驗證 Cookie,這會覆寫您在 事件處理程式 CreatingCookie 中建立的任何 Cookie。

適用於

另請參閱