.net core 2 及更高版本:连接服务 WcfServiceClient SOAP with NTLM Authorization 如何?

Waj*_*khi 1 wcf soap asp.net-core-2.0 ntlm-authentication

我正在 .net core 2.1 上运行一个应用程序。我通过已成功生成 WcfServiceClient 的连接服务添加了 wsdl Web 服务。

使用Basic Autorization 时,它工作正常

这是我用于调用 helloword soap 方法的类:

public string HellowWorld(string input)
{
    string wsRes = null;
    try
    {
        var service = new WorkerProcessServiceClient();
        var url = $"http://ServerUrl/Directory/WsName.svc";
        UriBuilder uriBuilder = new UriBuilder(url);

        service.Endpoint.Address = new EndpointAddress(uriBuilder.Uri);
        service.ClientCredentials.UserName.UserName = Username;
        service.ClientCredentials.UserName.Password = Password;

        using (OperationContextScope scope = new OperationContextScope(service.InnerChannel))
        {
            HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] =
                "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(service.ClientCredentials.UserName.UserName
                + ":"
                + service.ClientCredentials.UserName.Password));
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
            wsRes = service.HelloWorldAsync(input, RetailContext).GetAwaiter().GetResult();
            service.Close();
        }
    }
    catch (Exception ex)
    {
        wsRes = ex.Message;
    }
    return wsRes;
}
Run Code Online (Sandbox Code Playgroud)

这适用于在Basic Authorization上运行的服务器。我在SOAP UI 中使用相同的凭据,并且运行良好。我什至不需要指定 在此处输入图片说明

<==> 现在是问题 <=>

我有第二台使用NTLM Authorization运行的服务器。我做了这一切:'(但似乎没有任何效果。

1 -我改变了我的service.clientCredential.Usernameservice.clientCredential.Windows我加service.clientCredential.Windows.domain

2 - 我也将标题从 更改"Basic " + Convert..."Ntlm " + Convert...

3 - 我在标题中添加了域,并将其放在第一个和最后一个位置。

当我使用SOAP UI 时,它工作得很好。 在此处输入图片说明

我不知道该怎么办,请帮助。

Waj*_*khi 5

我终于发现了。

所以这里我的新代码通过 NTLM 授权获取服务

    private WcfServiceClient MyNtlmConfiguredService()
    {
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        //this is for enabling Ntlm if you wanna work with basic you just 
        // you just replace HttpClientCredentialType.Ntlm by HttpClientCredentialType.Basic
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

        EndpointAddress endpoint = new EndpointAddress("http://ServerUrl/Directory/WsName.svc");

        var client = new WcfServiceClient(basicHttpBinding, endpoint);

        NetworkCredential myCreds = new NetworkCredential("Username", "pas**rd", "Domain");

        client.ClientCredentials.Windows.ClientCredential = myCreds;
        client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

        return client;
    }
Run Code Online (Sandbox Code Playgroud)

然后你正常调用你的 WebService

MyNtlmConfiguredService().HellowWorld(input).getAwaiter().getResult();
Run Code Online (Sandbox Code Playgroud)

现在对于基本授权:

    private CustomerWcfServiceClient MyBasicConfiguredService()
    {
        var service = new CustomerWcfServiceClient();
        CustomerWcfServiceClient client = null;
        string wsRes = null;

        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;//mandatory
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;//mandatory

        EndpointAddress endpoint = new EndpointAddress("http://ServerUrl/Directory/WsName.svc");

        client = new CustomerWcfServiceClient(basicHttpBinding, endpoint);


        client.ClientCredentials.UserName.UserName = "UserName";
        client.ClientCredentials.UserName.Password = "Pa**word";

        return client;
    }
Run Code Online (Sandbox Code Playgroud)

然后你正常调用你的 WebService

MyBasicConfiguredService().HellowWorld(input).getAwaiter().getResult();
Run Code Online (Sandbox Code Playgroud)

祝大家编码愉快