我正在MVC应用程序中调用第三方服务,如果我在web.config中的端点中传递值,它将使该服务正常工作
<endpoint address="address of svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IETDocService" contract="EDocService.IETDocService" name="BasicHttpBinding_IETDocService">
<headers>
<UserNameHeader xmlns="urn:-----" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<UserName xmlns="http://schemas.datacontract.org/2007/08/FT.DHM">john.cena@dmh.com</UserName>
<Password xmlns="http://schemas.datacontract.org/2007/08/FT.DHM">32d698cf-51t5-4080-6d44-143e4432df73</Password>
</UserNameHeader>
</headers>
</endpoint>
此配置工作正常,现在我需要动态传递用户名和密码,但是它不起作用,下面是我尝试过的代码,
尝试1
var endAdd = new EndpointAddressBuilder(loginService.Endpoint.Address);
endAdd.Headers.Add(AddressHeader.CreateAddressHeader("UserName", string.Empty,
request.UserName));
endAdd.Headers.Add(AddressHeader.CreateAddressHeader("Password",
string.Empty,
request.Pwd));
loginService.Endpoint.Address = endAdd.ToEndpointAddress();
尝试2
ctWcf.ClientCredentials.ClientCertificate.Certificate = this.MessageSigningCertificate;
using (new OperationContextScope(ctWcf.InnerChannel))
{
UserInfo userInfo = new UserInfo();
userInfo.UserName = "john.cena@dmh.com";
userInfo.Password = "32d698cf-51t5-4080-6d44-143e4432df73";
MessageHeader aMessageHeader = MessageHeader.CreateHeader("UserNameHeader", "http://uddi.org", userInfo);
OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);
}
无法致电服务,非常感谢您的帮助