用于CXF Web服务的.NET客户端身份validation和SOAP凭据标头
情景
我必须使用.NET客户端访问Web服务。 该服务是Apache CXF Web服务。 需要用户名和密码validation。 我创建了代理。 我已经设置了凭证。
MyServiceReference proxy = new MyServiceReference(); proxy.Credentials = new NetworkCredential("username", "password"); string res = proxy.Method1();
当我运行客户端时,抛出以下exception:
System.Web.Services.Protocols.SoapHeaderException: An error was discovered processing the header
服务发布者告诉我,SOAP头中没有凭据。 所以,我猜IWebProxy.Credentials不是设置身份validation的正确方法。
题
那么,我如何设置身份validation所需的SOAP头?
最终我不得不调用创建整个SOAP消息的服务并创建一个HttpWebRequest
。 在SOAP消息中,我手动指定安全头:
Foo Bar qM6iT8jkQalTDfg/TwBUmA== 2012-06-28T15:49:09.497Z
这里的服务客户端:
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)的有趣资源:
- 维基百科
- 绿洲
- 用于签署Flickr API请求的无效签名(在控制台中进行模拟)
- UserPrincipal.FindByIdentity中的.NET 4.5错误(System.DirectoryServices.AccountManagement)
- 从登录和注销获得通知
- WebApi.Owin中的SuppressDefaultHostAuthentication还禁止webapi之外的身份validation
- 如何使用特定凭据连接到C#中的TFS服务器?
- C#SMTP身份validation失败,但凭据正确无误
- 阻止显示Windows安全窗口
- 将.net core 2的JWT Authentication实现转移到asp.net web api 2
- 如何在RESTful WCF API中实现HMAC身份validation