【发布时间】:2014-01-15 10:58:35
【问题描述】:
应用程序SoapUI 我为端点设置了密码和用户名。当您调用该方法时,这些数据不会在消息头中传输,而是在消息头之外传输(Authorization: Basic YTph)。但是,在这种情况下,调用已成功验证。如果我尝试在我的 c# 客户端应用程序的标头中添加身份验证数据,则服务看不到用户名。
SoapUI:
POST https://xxx.xx:9444/xx HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic YTph
Content-Length: 470
Host: xx.loc:9444
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ami="http://xxx.pl">
<soapenv:Header/>
<soapenv:Body>
<ami:getMeterPointStates>
<addr>
<ppec>0</ppec>
<mid>0</mid>
</addr>
<start>2012-04-01T00:00:00</start>
<stop>2012-05-01T00:00:00</stop>
<hard>false</hard>
</ami:getMeterPointStates>
</soapenv:Body>
</soapenv:Envelope>
C# 客户端示例代码:
System.Net.ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
CommunicationWebserviceClient client = new CommunicationWebserviceClient();
client.ClientCredentials.UserName.UserName = "user";
client.ClientCredentials.UserName.Password = "password";
using (new System.ServiceModel.OperationContextScope(client.InnerChannel))
{
MessageHeader head = MessageHeader.CreateHeader("Authorization", "http://xxx.pl", "Basic YTph");
OperationContext.Current.OutgoingMessageHeaders.Add(head);
MeterPointAddr addr = new MeterPointAddr();
addr.ppec = "0";
addr.mid = "0";
var response = client.getMeterPointStates(addr, new DateTime(2012, 4, 1), new DateTime(2012, 4, 14), false, "", measurementsFlagEnum.REAL_ESTIMATION);
}
App.config:
<bindings>
<customBinding>
<binding name="CommunicationWebservicePortBinding">
<textMessageEncoding messageVersion="Soap11" />
<httpsTransport authenticationScheme="Basic" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://xxx.xx:9444/xx"
binding="customBinding" bindingConfiguration="CommunicationWebservicePortBinding" contract="AMIService.CommunicationWebservice"
name="CommunicationWebservicePort" />
</client>
如何添加用 c# 编写的客户端 (Authorization: Basic YTph) 标头?
【问题讨论】:
-
我猜在您的 C# 客户端代码中,您不需要添加
MessageHeader.CreateHeader("Authorization" ...行。当您将身份验证设置为基本并使用用户名和密码时,会自动生成此行。YTph实际上是您的用户名和密码,采用 base-64 编码。如果这是生产代码,请确保使用 SSL。 -
它对我有用。 stackoverflow.com/questions/20495105/…有代码示例。