Preemptive authentication with CustomBinding?

南楼画角 提交于 2019-12-08 06:15:21

问题


I'm writing a client against a customer's SOAP service, using WCF.

We've had a number of go-arounds trying to get the authentication working. I ended up using a Custom Binding, because some random guy on the web said that BasicHttpBinding didn't support the necessary security options, and WsHttpBinding didn't support SOAP 1.1, which is what they are using.

So, what I have:

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;
}

Now, I've been asked if I can configure my client to do preemptive authentication. I've done some web browsing, and not found much. And I'm at a loss as to how to integrate what little I've found into my current code.


回答1:


I don't think you can configure WCF to pre authenticate. Your options are to add the headers manually to each request or to build a message inspector to do it and configure it once. Either way those settings are not related to the binding. I guess you could write your own custom http transport (that internally uses the regular http transport) and add it there but not sure it worth the effort. As described here, to add it manually you can use:

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 
}

As for the second option, this see here how to add headers with a message inspector.



来源:https://stackoverflow.com/questions/26366045/preemptive-authentication-with-custombinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!