Windows Communication Foundation (WCF) では、ユーザー名とパスワードを認証に使用すると、既定の Windows 認証を使用してユーザー名とパスワードが検証されます。ただし、WCF では、ユーザー名およびパスワードのカスタム認証方式を使用することもできます。このような認証方式は、検証と呼ばれます。ユーザー名およびパスワードのカスタム検証を組み込むには、UserNamePasswordValidator から派生するクラスを作成して構成します。
サンプル アプリケーションについては、「UserNamePassword 検証」を参照してください。
方法 : ユーザー名およびパスワードのカスタム検証を作成するには
-
UserNamePasswordValidator から派生するクラスを作成します。
Visual BasicPublic Class CustomUserNameValidator Inherits UserNamePasswordValidator
C#public class CustomUserNameValidator : UserNamePasswordValidator {
-
Validate メソッドをオーバーライドすることにより、カスタム認証方式を実装します。
次のコードは、Validate メソッドをオーバーライドします。本運用環境では、このようなコードを使用しないでください。このコードは、ユーザー名およびパスワードのカスタム検証方式で置き換える必要があります。このとき、ユーザー名とパスワードの組み合わせをデータベースから取得します。
Visual Basic' This method validates users. It allows in two users, test1 and test2 ' with passwords 1tset and 2tset respectively. ' This code is for illustration purposes only and ' MUST NOT be used in a production environment becuase it is NOT secure. Public Overrides Sub Validate(ByVal userName As String, ByVal password As String) If Nothing = userName OrElse Nothing = password Then Throw New ArgumentNullException() End If If Not (userName = "test1" AndAlso password = "1tset") AndAlso Not (userName = "test2" AndAlso password = "2tset") Then Throw New SecurityTokenException("Unknown Username or Incorrect Password") End If End Sub
C#// This method validates users. It allows in two users, test1 and test2 // with passwords 1tset and 2tset respectively. // This code is for illustration purposes only and // MUST NOT be used in a production environment becuase it is NOT secure. public override void Validate(string userName, string password) { if (null == userName || null == password) { throw new ArgumentNullException(); } if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset")) { throw new SecurityTokenException("Unknown Username or Incorrect Password"); } }
ユーザー名およびパスワードのカスタム検証を使用するようにサービスを構成するには
-
メッセージ セキュリティを指定し、資格情報の種類を UserName に指定するバインディングを構成します。
定義済みのバインディングのいずれか (wsHttpBinding 要素など)、またはメッセージ セキュリティと資格情報の種類 UserName をサポートする customBinding 要素を追加します。
-
構成ファイルの <system.ServiceModel> 要素の下に、<bindings> 要素を追加します。
-
wsHttpBinding 要素をバインディング セクションに追加します。WCF バインディング要素の作成の詳細については、「方法 : 構成でサービス バインディングを指定する」を参照してください。
-
wsHttpBinding の security 要素の mode 属性を Message に設定します。
-
wsHttpBinding の message 要素の clientCredentialType 属性を UserName に設定します。これにより、ユーザー名とパスワードの組み合わせがクライアントの資格情報として使用されます。
WCF バインディング要素の作成の詳細については、「方法 : 構成でサービス バインディングを指定する」を参照してください。
次のコード例は、バインディングの構成コードを示しています。
<system.serviceModel> <bindings> <wsHttpBinding> <binding name="Binding1"> <security mode="Message"> <message clientCredentialType="UserName" /> </security> </binding> </wsHttpBinding> </bindings> </system.serviceModel> -
-
受信 UserNameSecurityToken セキュリティ トークンのユーザー名とパスワードの組み合わせの検証に、ユーザー名およびパスワードのカスタム検証を使用することを指定する動作を構成します。
-
<system.serviceModel> 要素の子として、<behaviors> 要素を追加します。
-
serviceBehaviors セクションを <behaviors> 要素に追加します。
-
<behavior> 要素を追加し、name 属性を適切な値に設定します。
-
<serviceCredentials> 要素を <behavior> 要素に追加します。
-
userNamePasswordValidationMode を Custom に設定します。
重要 : userNamePasswordValidationMode 値が設定されていない場合、WCF では、ユーザー名およびパスワードのカスタム検証の代わりに Windows 認証が使用されます。
-
customUserNamePasswordValidatorType を、ユーザー名およびパスワードのカスタム検証を表す種類を設定します。
次の例に、この時点での <serviceCredentials> のフラグメントを示します。
<serviceCredentials> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" /> </serviceCredentials>
-
使用例
ユーザー名およびパスワードのカスタム検証を作成する方法を次のコード例に示します。本運用環境では、Validate メソッドをオーバーライドするコードを使用しないでください。このコードは、ユーザー名およびパスワードのカスタム検証方式で置き換える必要があります。このとき、ユーザー名とパスワードの組み合わせをデータベースから取得します。
Imports System Imports System.IdentityModel.Selectors Imports System.IdentityModel.Tokens Imports System.Security.Principal Imports System.ServiceModel ... Public Class CustomUserNameValidator Inherits UserNamePasswordValidator ' This method validates users. It allows in two users, test1 and test2 ' with passwords 1tset and 2tset respectively. ' This code is for illustration purposes only and ' MUST NOT be used in a production environment becuase it is NOT secure. Public Overrides Sub Validate(ByVal userName As String, ByVal password As String) If Nothing = userName OrElse Nothing = password Then Throw New ArgumentNullException() End If If Not (userName = "test1" AndAlso password = "1tset") AndAlso Not (userName = "test2" AndAlso password = "2tset") Then Throw New SecurityTokenException("Unknown Username or Incorrect Password") End If End Sub End Class
using System; using System.IdentityModel.Selectors; using System.IdentityModel.Tokens; using System.Security.Principal; using System.ServiceModel; ... public class CustomUserNameValidator : UserNamePasswordValidator { // This method validates users. It allows in two users, test1 and test2 // with passwords 1tset and 2tset respectively. // This code is for illustration purposes only and // MUST NOT be used in a production environment becuase it is NOT secure. public override void Validate(string userName, string password) { if (null == userName || null == password) { throw new ArgumentNullException(); } if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset")) { throw new SecurityTokenException("Unknown Username or Incorrect Password"); } } }