5

シナリオ

.NETクライアントを使用してWebサービスにアクセスする必要があります。このサービスは、ApacheCXFWebサービスです。ユーザー名とパスワードの認証が必要です。プロキシを作成しました。クレデンシャルを設定しました。

MyServiceReference proxy = new MyServiceReference();
proxy.Credentials = new NetworkCredential("username", "password");
string res = proxy.Method1();

クライアントを実行すると、次の例外がスローされます。

System.Web.Services.Protocols.SoapHeaderException: An error was discovered processing the <wsse:Security> header

サービス発行者は、資格情報がSOAPヘッダーに存在しないと私に言いました。したがって、IWebProxy.Credentialsは認証を設定する正しい方法ではないと思います。

質問

では、認証に必要なSOAPヘッダーを設定するにはどうすればよいですか?

4

1 に答える 1

5

最終的には、サービスを呼び出して、SOAPメッセージ全体を作成し、を作成する必要がありましたHttpWebRequest。SOAPメッセージで、セキュリティヘッダーを手動で指定します。

<soapenv:Header>
  <wsse:Security soapenv:mustUnderstand='1' xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>
     <wsse:UsernameToken wsu:Id='UsernameToken-1' xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>
        <wsse:Username>Foo</wsse:Username>
        <wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>Bar</wsse:Password>
        <wsse:Nonce EncodingType='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'>qM6iT8jkQalTDfg/TwBUmA==</wsse:Nonce>
        <wsu:Created>2012-06-28T15:49:09.497Z</wsu:Created>
     </wsse:UsernameToken>
  </wsse:Security>
</soapenv:Header>

そしてここにサービスクライアント:

String Uri = "http://web.service.end.point"
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Uri);
req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";

String SoapMessage = "MySoapMessage, including envelope, header and body"
using (Stream stm = req.GetRequestStream())
{
    using (StreamWriter stmw = new StreamWriter(stm))
    {
        stmw.Write(SoapMessage);
    }
}


try
{
    WebResponse response = req.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    log.InfoFormat("SoapResponse: {0}", sr.ReadToEnd());
}
catch(Exception ex)
{
    log.Error(Ex.ToString());
}

Webサービスセキュリティ(WSS)に関する興味深いリソース:

于 2012-07-02T13:12:43.960 に答える