これらのサービスは、.net 1.1 および .net 2.0 を使用してアクセスできる必要があります。この目的のために、basicHttpBinding を使用しています。
古い ASMX Web サービスでは、リクエストごとにユーザーを認証するために 1 つの Soap ヘッダー (AuthHeader) を割り当てました。basicHttpBinding
を使用して WCF で認証するにはどうすればよいですか? サンプルまたはチュートリアルは役に立ちます。
nRk
これらのサービスは、.net 1.1 および .net 2.0 を使用してアクセスできる必要があります。この目的のために、basicHttpBinding を使用しています。
古い ASMX Web サービスでは、リクエストごとにユーザーを認証するために 1 つの Soap ヘッダー (AuthHeader) を割り当てました。basicHttpBinding
を使用して WCF で認証するにはどうすればよいですか? サンプルまたはチュートリアルは役に立ちます。
nRk
WCFに切り替える前と同じようにAuthHeaderを使用できます。多分それはあなたにとってより便利でしょう、なぜなら原理は同じままであるからです。このソリューションで私が目にする悪い点は、プレーンテキストのパスワード転送です。とにかく、それは単なる別のオプションであり、なんとかしてパスワードを暗号化/復号化することができます。
この場合、次のように、独自のIDispatchMessageInspectorとIClientMessageInspectorを実装する必要があります。
[AttributeUsage(AttributeTargets.Class)]
public class CredentialsExtractorBehaviorAttribute : Attribute, IContractBehavior, IDispatchMessageInspector
{
#region IContractBehavior implementation.
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(this);
}
... empty interface methods impl skipped ...
#endregion
#region IDispatchMessageInspector implementation.
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
int i = request.Headers.FindHeader("username", "sec");
if (-1 != i)
{
string username = request.Headers.GetHeader<string>("username", "sec");
... do smth ...
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
return;
}
#endregion
}
サンプルでは、ユーザー名のみをヘッダーに配置しましたが、ユーザー名とパスワードを含むクラスを実装して、文字列の代わりに使用できます。クライアントの場合:
internal class CredentialsInserter : IContractBehavior, IClientMessageInspector
{
private string m_username;
public CredentialsInserter(string username)
{
m_username = username;
}
#region IContractBehavior implementation.
... empty interface methods impl skipped ...
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
#endregion
#region IClientMessageInspector implementation.
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageHeader<string> mh = new MessageHeader<string>(m_username);
request.Headers.Add(mh.GetUntypedHeader("username", "sec"));
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
return;
}
#endregion
}
次に、属性CredentialsExtractorBehaviorAttributeをサービス実装クラスに配置する必要があります。
[CredentialsExtractorBehavior]
public class DummyService : IDummyService
{
... impl ...
}
また、クライアント側では、次のことを行う必要があります。
using (DummyServiceClient c = new DummyServiceClient("TcpEndpoint"))
{
c.ChannelFactory.Endpoint.Contract.Behaviors.Add(
new CredentialsInserter("_username_"));
c.DummyMethod();
}
まず第一に - はい、できます!トランスポートまたはメッセージ バインディングのどちらを使用するかによって異なります。インターネットに接続している場合は、メッセージ ベースのセキュリティを使用する可能性が高くなります。
残念ながら、メッセージベースのセキュリティの場合、basicHttpBinding は証明書のみをサポートするため、少し面倒です。
一方、wsHttpBinding は、ユーザー名/パスワードまたはその他のメソッドもサポートします。
次のように、メッセージベースのセキュリティでユーザー名/パスワードのクライアント資格情報を使用して wsHttpBinding を構成します。
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsUserName">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="yourservice">
<endpoint name="YourEndpoint"
address=""
binding="wsHttpBinding"
bindingConfiguration="wsUserName"
contract="IYourService" />
</service>
</services>
</system.serviceModel>
以下のセクションでは、ユーザー名/パスワードのクライアント資格情報で message-security を使用する wsHttpBindingのバインド構成<bindings>
を定義します。
以下のセクションで<service>
は、wsHttpBinding を使用し、先ほど定義したバインディング構成を参照するサンプル サービスを定義します。
サーバー側では、ネットワーク経由で送信されるユーザー名/パスワードを使用して、Active Directory (すべての呼び出しに AD アカウントが必要です) または ASP.NET メンバーシップ システム データベースで発信者を検証できます。または、どうしても必要な場合は、独自の認証メカニズムを作成することもできます。
Codeplexで WCF セキュリティに関する有益な情報を多数見つけてください - 優れたリソースです。
こちらのシナリオを確認して、状況に合わせてください。各シナリオには、ソリューションの実装に必要なアイテムのチェックリストが用意されています。