Как обрабатывать проверку подлинности NTLM с помощью клиента SOAP Webservice? - PullRequest
0 голосов
/ 10 января 2019

Я пытаюсь выполнить аутентификацию NTLM с клиентским объектом веб-сервиса SOAP , но происходит сбой со следующим исключением:

HTTP-запрос не авторизован с помощью схемы аутентификации клиента 'Ntlm'. Заголовок аутентификации, полученный от сервера, был «NTLM ### Мой полученный токен ###».

При выполнении запроса с HttpWebRequest Object он работает нормально.

Вот мой пример кода клиента:

class Program {

    static void Main(string[] args) {

        EndpointAddress endpointAddress = null;
        BasicHttpBinding basicClientBinding = null;
        HttpRequestMessageProperty httpRequestProperty = null;

        myWebObject myObject = new myWebObject() { Id = new Guid(), Attribute1 = "Value 1" };
        myWebServiceClient myServiceClient = null;
        update myUpdateObject = new update();
        myUpdateObject.customObject = myObject;
        updateResponse myResponse = null;

        HttpWebRequest request = null;
        WebResponse response = null;
        string sb = string.Empty;

        string username = "Username";
        string password = "Password";

        //Endpoint
        endpointAddress = new EndpointAddress("https://mydomain/endpoint?wsdl");

        //avoid SLL Certificate Errors
        System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

        //use TLS 1.2
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        //Basic HTTP Binding
        basicClientBinding = new BasicHttpBinding();
        basicClientBinding.Name = "BasicHttpBinding";
        if (endpointAddress.Uri.Scheme == "https") {
            basicClientBinding.Security.Mode = BasicHttpSecurityMode.Transport;
        } else {
            basicClientBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        }
        basicClientBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
        basicClientBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

        //SOAP Client
        myServiceClient = new myWebServiceClient(basicClientBinding, endpointAddress);
        myServiceClient.ClientCredentials.UserName.UserName = username;
        myServiceClient.ClientCredentials.UserName.Password = password;

        //HttpWebRequest
        request = (HttpWebRequest)HttpWebRequest.Create(endpointAddress.Uri);
        request.Method = "POST";

        //NTLM Authentication
        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(endpointAddress.Uri, "NTLM", new NetworkCredential(username, password));
        request.Credentials = credentialCache;

        //add Custom Header
        request.Headers.Add(
                "MyAuthorization",
                "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(
                    username + ":" + password)
                )
            );

        sb = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><update xmlns=\"http://myservice.webservice.product.mydomain.com/\"><object Id=\"00000000-0000-0000-0000-000000000000\" xmlns=\"http://product.mydomain.com/myservice/object\"><Attribute1>Value 1</Attribute1></object></update></s:Body></s:Envelope>";        
        Stream firstStream = request.GetRequestStream();
        using (Stream st = firstStream) {
            using (StreamWriter writer = new StreamWriter(st)) {
                writer.Write(sb);
            }
        }

        try {

            if (args.Length > 0) {

                //This Request WORKS

                response = request.GetResponse();   

            } else {

                //This Request FAILS

                myServiceClient.Open();             

                using (OperationContextScope scope = new OperationContextScope(myServiceClient.InnerChannel)) {

                    //add Custom Header
                    httpRequestProperty = new HttpRequestMessageProperty();
                    httpRequestProperty.Headers.Add("MyAuthorization",
                        "Basic " +
                        Convert.ToBase64String(Encoding.UTF8.GetBytes(
                            username + ":" + password)));
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                    //Calling Webservice            
                    myResponse = myServiceClient.update(myUpdateObject);        

                }

                myServiceClient.Close();    

            }

        } catch (Exception ex) {
            throw new Exception(ex.Message);
        }
    }
}
...