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 属性才能检索值。

适用于