ユーザー名/パスワードを使用してWCFでWebサービス認証に接続する
-
03-07-2019 - |
質問
Visual Studio 2008でWebサービスのプロキシを作成し、app.configに次のエントリを作成しました:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyNameHandlerSoapBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.***/***/***"
binding="basicHttpBinding" bindingConfiguration="MyNameHandlerSoapBinding"
contract="***.MyNameHandler" name="MyName">
</endpoint>
</client>
</system.serviceModel>
ウェブサービスにはユーザー名/パスワード認証があるため、ここに追加する必要があります。
WCFドキュメントの海で少し迷っています。認証要素を追加するには、basicHttpBindingからwsHttpBindingまたはcustomBindingに変更する必要があると思いますが、実際には理解できません。誰でも簡単なヒントや方法を説明する便利なリンクを教えてもらえますか?
編集:
セキュリティセクションを次のように変更しました:
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="None"
realm="" />
</security>
およびコードに追加:
ws.ClientCredentials.UserName.UserName = "";
ws.ClientCredentials.UserName.Password = "";
今では資格情報を使用しているようですが、エラーが発生しています:
提供されたURIスキーム 'http'は無効なURI 'https'である必要があります
これが正しい方法かどうかさえわかりません...
解決
将来の読者のためのソリューションをここに投稿します:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyHandlerSoapBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.***/***/***/MyHandler"
binding="basicHttpBinding" bindingConfiguration="MyHandlerSoapBinding"
contract="***.MyHandler" name="MyHandler">
</endpoint>
</client>
</system.serviceModel>
最後に、デフォルトのbasicHttpBindingを使用できました。質問に投稿されたコードとの唯一の違いは、セキュリティノードです。
mode =&quot; TransportCredentialOnly&quot; オプションにも注意してください。これにより、 https
の代わりに http
を使用してユーザー名/パスワードを送信できます。これは、私が使用している環境をテストするために必要です。明らかに、資格情報の送信には https
を好むでしょう。
その後、コードでユーザー名/パスワードを入力します:
var ws = new ***.MyHandlerClient("MyHandler");
ws.ClientCredentials.UserName.UserName = "myUsername";
ws.ClientCredentials.UserName.Password = "myPassword";
var result = ws.executeMyMethod();
他のヒント
エラーメッセージは正しいです。 WCFは、保護されていないプロトコルを介したユーザー名とパスワードの転送を許可しません。 Webサービスは、HTTPS(アテンダントSSL証明書付き)を使用する必要があります
SSL証明書を取得したら、資格情報の送信方法(トランスポートまたはセキュリティ)の2つのオプションと、資格情報の種類の複数のオプションがあります。 MSDNには、さまざまなオプションすべてに対する優れたガイドがあります。
>同じ問題があり、上記の解決策を試しました どういうわけかこれは私のために機能しませんでした 「WS-Securityヘッダーが見つかりません」というメッセージが引き続き表示される
テストと試行の長い時間の後、私はそれを機能させることができます 以下のようにクライアントにヘッダーコードを追加すると、動作します!
<client>
<endpoint address="http://your.service.com" binding="basicHttpBinding" bindingConfiguration="XXXBinding" contract="contract.XXX" name="XXXPort">
<headers xmlns:wsse="http://your.xsd">
<wsse:Security mustUnderstand="1">
<wsse:UsernameToken>
<tenant>XXX</tenant>
<wsse:Username>XXX</wsse:Username>
<wsse:Password Type="http://www.xxxx.com/wss#PasswordText">XXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint>
</client>