次の方法で共有


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 イベントのイベント ハンドラーを示しています。 ハンドラーは、認証 Cookie をカスタマイズするために、 CreatingCookieEventArgs オブジェクトからユーザー値を取得します。 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 メソッドに渡される値が含まれています。 通常、このプロパティは、ID 番号などのユーザー名とパスワードで検証する必要があるカスタム値を渡すために使用されます。 プロパティに複数の値が格納されている場合は、値を取得するために CustomCredential プロパティを解析する必要があります。

適用対象