次の方法で共有


AuthenticationService.CreatingCookie イベント

定義

認証 Cookie が設定されているときに発生します。

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) 

イベントの種類

次の例は、Global.asax ファイルの Application_Start メソッドのCreatingCookie イベントにイベント ハンドラーをバインドする方法を示しています。

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

次の例は、Global.asax ファイル内の CreatingCookie イベントのイベント ハンドラーを示しています。 イベント ハンドラーは、 CustomCredential プロパティの値を UserData プロパティに追加することで、認証 Cookie をカスタマイズします。 プロパティ内のデータが機密でないことがわかっている場合にのみ、 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

注釈

CreatingCookie イベントは、ユーザー資格情報の検証後に認証 Cookie が設定されているときに発生します。 認証 Cookie をカスタマイズするために、 CreatingCookie イベントのイベント ハンドラーを作成します。

適用対象

こちらもご覧ください