CreatingCookieEventArgs.CustomCredential 屬性

定義

取得使用者所提供的其他驗證值。

public:
 property System::String ^ CustomCredential { System::String ^ get(); };
public string CustomCredential { get; }
member this.CustomCredential : string
Public ReadOnly Property CustomCredential As String

屬性值

驗證需要使用者名稱和密碼以外的自訂值。

範例

下列範例顯示 事件的事件處理常式 CreatingCookie 。 處理常式會從 CreatingCookieEventArgs 物件擷取使用者值,以自訂驗證 Cookie。 傳入 CustomCredential 屬性的值會儲存在 UserData 表單驗證票證的 屬性中。

注意

CustomCredential只有在您知道屬性中的資料不敏感時,才會將屬性儲存在 Cookie 中。 惡意使用者可以存取 Cookie 中的值。

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

備註

您可以使用 CustomCredential 屬性來擷取驗證票證中的自訂值。 屬性 CustomCredential 包含傳遞至 Login 方法的值。 一般而言,這個屬性是用來傳遞必須使用使用者名稱和密碼來驗證的自訂值,例如識別碼。 如果屬性中儲存多個值,您必須剖析 CustomCredential 屬性,才能擷取值。

適用於