次の方法で共有


方法 : WCF 認証サービスを使用するときのユーザー ログインをカスタマイズする

更新 : 2007 年 11 月

ここでは、Windows Communication Foundation (WCF) を使用して ASP.NET 認証サービスを呼び出すときに、カスタマイズした資格情報を検証してユーザーを認証する方法について説明します。通常、認証に必要なのはユーザー名とパスワードだけです。しかし、その他の資格情報 (識別番号など) を使用してユーザーの ID を検証する必要がある場合もあります。

SOAP 1.1 メッセージを送信および処理できるクライアント アプリケーション (Java アプリケーションなど) からユーザーをログインさせる場合、WCF 実装の認証サービスを使用します。

認証用のカスタマイズした資格情報を検証するには

  1. Web アプリケーションの Global.asax ファイルで、Authenticating イベントのイベント ハンドラを作成します。

  2. ハンドラで、ハンドラの AuthenticatingEventArgs パラメータの CustomCredential プロパティの内容を読み取り、値を認証します。

    認証する 2 つの値を CustomCredential プロパティから受け取り、StudentAuthentication という名前のカスタム認証クラスに渡す方法の例を次に示します。

    Sub AuthenticationService_Authenticating _
       (ByVal sender As Object, _
        ByVal e As System.Web.ApplicationServices.AuthenticatingEventArgs)
        Dim studentid As String = String.Empty
        Dim answer As String = String.Empty
    
        Dim credentials As String() = _
             e.CustomCredential.Split(New Char() {","c})
        If (credentials.Length > 0) Then
            studentid = credentials(0)
            If (credentials.Length > 1) Then
                answer = credentials(1)
            End If
        End If
    
        Try
            e.Authenticated = _
                StudentAuthentication.ValidateStudentCredentials _
                (e.Username, e.Password, studentid, answer)
        Catch ex As ArgumentNullException
            e.Authenticated = False
        End Try
    
    
        e.AuthenticationIsComplete = True
    End Sub
    
    void AuthenticationService_Authenticating(object sender, System.Web.ApplicationServices.AuthenticatingEventArgs e)
    {
        string studentid = String.Empty;
        string answer = String.Empty;
    
        string[] credentials =
            e.CustomCredential.Split(new char[] { ',' });
        if (credentials.Length > 0)
        {
            studentid = credentials[0];
            if (credentials.Length > 1)
            {
                answer = credentials[1];
            }
        }
    
        try
        {
            e.Authenticated =
                StudentAuthentication.ValidateStudentCredentials
                (e.UserName, e.Password, studentid, answer);
        }
        catch (ArgumentNullException ex)
        {
            e.Authenticated = false;
        }
    
        e.AuthenticationIsComplete = true;
    }
    
  3. Global.asax ファイルの Application_Start メソッドで、イベント ハンドラを Authenticating イベントに関連付けます。

    次の例では、イベント ハンドラを Authenticating イベントに関連付ける方法を示します。

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        AddHandler System.Web.ApplicationServices.AuthenticationService.Authenticating, _
          AddressOf Me.AuthenticationService_Authenticating
    End Sub
    
    void Application_Start(object sender, EventArgs e) 
    {
        System.Web.ApplicationServices.AuthenticationService.Authenticating += 
            new EventHandler<System.Web.ApplicationServices.AuthenticatingEventArgs>(AuthenticationService_Authenticating);
    
    }
    
  4. Web サービスからの SOAP メッセージを処理できるアプリケーションから認証サービスを呼び出し、認証する他の値を CustomCredential プロパティで渡します。

コードのコンパイル方法

堅牢性の高いプログラム

前のコード例では、パラメータが null の場合に ArgumentNullException をスローするカスタム認証クラスを示しています。コードでは、検証中に発生したすべての例外を処理する必要があります。

セキュリティ

認証サービスには、常に SSL (Secure Sockets Layer) を使用して HTTPS プロトコルによってアクセスします。

参照

概念

Windows Communication Foundation の認証サービスの概要

参照

AuthenticationService

AuthenticatingEventArgs