AuthenticationService.CreatingCookie Événement

Définition

Se produit lorsque le cookie d'authentification est défini.

public:
 static event EventHandler<System::Web::ApplicationServices::CreatingCookieEventArgs ^> ^ CreatingCookie;
public static event EventHandler<System.Web.ApplicationServices.CreatingCookieEventArgs> CreatingCookie;
member this.CreatingCookie : EventHandler<System.Web.ApplicationServices.CreatingCookieEventArgs> 
Public Shared Custom Event CreatingCookie As EventHandler(Of CreatingCookieEventArgs) 

Type d'événement

Exemples

L’exemple suivant montre comment lier un gestionnaire d’événements à l’événement CreatingCookie dans la Application_Start méthode du fichier Global.asax.

void Application_Start(object sender, EventArgs e)
{
    System.Web.ApplicationServices.AuthenticationService.CreatingCookie 
        += new EventHandler<System.Web.ApplicationServices.CreatingCookieEventArgs>
        (AuthenticationService_CreatingCookie);
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    AddHandler System.Web.ApplicationServices.AuthenticationService.CreatingCookie, _
        AddressOf Me.AuthenticationService_CreatingCookie
End Sub

L’exemple suivant montre un gestionnaire d’événements pour l’événement CreatingCookie dans le fichier Global.asax. Le gestionnaire d’événements personnalise le cookie d’authentification en ajoutant la valeur de la CustomCredential propriété à la UserData propriété . Stockez la CustomCredential propriété dans un cookie uniquement si vous savez que les données de la propriété ne sont pas sensibles. Les utilisateurs malveillants peuvent accéder aux valeurs du 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

Remarques

L’événement CreatingCookie est déclenché lorsque le cookie d’authentification est défini après la validation des informations d’identification de l’utilisateur. Créez un gestionnaire d’événements pour l’événement CreatingCookie afin de personnaliser le cookie d’authentification.

S’applique à

Voir aussi