CreatingCookieEventArgs.CustomCredential Property

Definition

Gets additional authentication values that are provided by the user.

C#
public string CustomCredential { get; }

Property Value

The custom values required for authentication, other than user name and password.

Examples

The following example shows an event handler for the CreatingCookie event. The handler retrieves user values from the CreatingCookieEventArgs object in order to customize the authentication cookie. The value passed in the CustomCredential property is stored in the UserData property of the forms authentication ticket.

Poznámka

Store the CustomCredential property in a cookie only when you know that the data in the property is not sensitive. Malicious users can access the values in the cookie.

C#
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;
}

Remarks

You use the CustomCredential property to retrieve custom values in the authentication ticket. The CustomCredential property contains the value passed to the Login method. Typically, this property is used to pass custom values that must be validated with the user name and password, such as an identification number. If more than one value is stored in the property, you must parse the CustomCredential property in order to retrieve values.

Applies to

Produkt Verzie
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1