2012-06-29 74 views
4

情景了CXF Web服務

.NET客戶端身份驗證和SOAP憑證頭我有一個.NET客戶端來訪問Web服務。 該服務是Apache CXF Web服務。 用戶名和密碼認證是必需的。 我已經創建了代理。 我已經設置了憑證。

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頭?

回答

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> 

而且這裏的服務客戶:

關於Web服務安全
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()); 
} 

有趣的資源(WSS):