Jef*_*ege 1 wcf soap basic-authentication
我正在使用WCF针对客户的SOAP服务编写客户端.
我们已经进行了多次复飞,试图让身份验证正常运行.我最终使用了Custom Binding,因为Web上的一些随机人员说BasicHttpBinding不支持必要的安全选项,而且WsHttpBinding不支持SOAP 1.1,这就是他们正在使用的.
那么,我有什么:
var message = this.constructMessagecollection);
if (message != null)
{
var ea = new EndpointAddress(this.webServiceUrl);
var binding = new CustomBinding();
binding.Elements.Add(new TextMessageEncodingBindingElement(
MessageVersion.Soap11, Encoding.UTF8));
binding.Elements.Add(new HttpsTransportBindingElement { AuthenticationScheme = System.Net.AuthenticationSchemes.Basic });
using (var client = new CustomersWebserviceClient(binding, ea))
{
if (!String.IsNullOrWhiteSpace(this.webServiceUsername) && !String.IsNullOrWhiteSpace(this.webServicePassword))
{
var credentials = client.ClientCredentials.UserName;
credentials.UserName = this.webServiceUsername;
credentials.Password = this.webServicePassword;
}
var result = client.ReceiveMessage(message);
log.writeLine(String.Format("Call to client.ReceiveMessage() returned {0}", result));
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
现在,我被问到是否可以将我的客户端配置为执行抢占式身份验证.我做了一些网页浏览,但没有找到太多.而且我不知道如何将我发现的很少的东西整合到我当前的代码中.
我认为您不能将WCF配置为进行身份验证.您可以选择手动将标头添加到每个请求,或构建消息检查器以执行此操作并配置一次.无论哪种方式,这些设置都与绑定无关.我想你可以编写自己的自定义http传输(内部使用常规的http传输)并将其添加到那里,但不确定它是否值得付出努力.如上所述这里,手动添加它,你可以使用:
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" +
client.ClientCredentials.UserName.Password));
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] =
httpRequestProperty;
// Invoke client
}
Run Code Online (Sandbox Code Playgroud)
至于第二个选项,这里看到如何使用消息检查器添加标头.