.net core 2及更高版本:具有NTLM授权的连接服务WcfServiceClient SOAP如何?

.net core 2及更高版本:具有NTLM授权的连接服务WcfServiceClient SOAP如何?,wcf,soap,asp.net-core-2.0,ntlm-authentication,Wcf,Soap,Asp.net Core 2.0,Ntlm Authentication,我正在.NETCore2.1上运行一个应用程序。 我通过成功生成WcfServiceClient的连接服务添加了wsdl web服务 当使用基本自动化时,它工作良好 下面是我用来调用helloword soap方法的类: public string HellowWorld(string input) { string wsRes = null; try { var service = new WorkerProcessServiceClient();

我正在.NETCore2.1上运行一个应用程序。 我通过成功生成WcfServiceClient的连接服务添加了wsdl web服务

当使用基本自动化时,它工作良好

下面是我用来调用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;
}
这在运行基本授权的服务器上运行良好。我在soapui中使用了相同的凭据,它工作得非常好。我甚至不需要指定

现在问题来了

我有第二台服务器,它使用NTLM授权运行。 我做到了这一切:“但似乎什么都不管用

1-我将service.clientCredential.Username更改为service.clientCredential.Windows,并添加了service.clientCredential.Windows.domain

2-我也从基本+转换更改了标题。。。要将Ntlm+转换为

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

当我使用soapui时,它工作得很好。


我不知道还能做什么,请帮助。

对于Windows身份验证,.net核心应用程序通过运行标识运行,例如,当您在IIS中托管时,它通过应用程序标识运行

这里有两个选项供您选择:

配置在域帐户用户下运行的.net core应用程序。 如果希望在代码中配置用户名和密码,可以尝试WindowsIdentity.RunImpersonated


对于Windows身份验证,传递运行标识的.net核心应用程序,例如,当您在IIS中托管时,它通过应用程序标识运行

这里有两个选项供您选择:

配置在域帐户用户下运行的.net core应用程序。 如果希望在代码中配置用户名和密码,可以尝试WindowsIdentity.RunImpersonated


我终于发现了

因此,这里是我的新代码,用于获取具有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;
    }
然后你可以正常调用你的Web服务

MyNtlmConfiguredService().HellowWorld(input).getAwaiter().getResult();
MyBasicConfiguredService().HellowWorld(input).getAwaiter().getResult();
现在进行基本授权:

    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;
    }
然后你可以正常调用你的Web服务

MyNtlmConfiguredService().HellowWorld(input).getAwaiter().getResult();
MyBasicConfiguredService().HellowWorld(input).getAwaiter().getResult();

我终于找到了答案

因此,这里是我的新代码,用于获取具有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;
    }
然后你可以正常调用你的Web服务

MyNtlmConfiguredService().HellowWorld(input).getAwaiter().getResult();
MyBasicConfiguredService().HellowWorld(input).getAwaiter().getResult();
现在进行基本授权:

    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;
    }
然后你可以正常调用你的Web服务

MyNtlmConfiguredService().HellowWorld(input).getAwaiter().getResult();
MyBasicConfiguredService().HellowWorld(input).getAwaiter().getResult();

快乐地为每个人编码

这似乎对我不起作用。但是谢谢,我发现它似乎对我不起作用。但是感谢我发现它在两种方法中都出现了这个错误,提供的URI方案“https”无效;应为“http”。参数名称:通过at System.ServiceModel.Channel.TransportChannelFactory1.ValidateSchemeUri通过at System.ServiceModel.Channel.HttpChannelFactory1.ValidateCreateChannelParametersEndpointAddress远程地址,Uri通过at System.ServiceModel.Channel.HttpChannelFactory1.OnCreateChannelCoreEndpointAddress远程地址,Uri通过System.ServiceModel.Channels.ChannelFactoryBase1.InternalCreateChannelEndpointAddress地址,Uri通过。。。。在提供的URI方案“https”的两种方法中都出现此错误是无效的;应为“http”。参数名称:通过at System.ServiceModel.Channel.TransportChannelFactory1.ValidateSchemeUri通过at System.ServiceModel.Channel.HttpChannelFactory1.ValidateCreateChannelParametersEndpointAddress远程地址,Uri通过at System.ServiceModel.Channel.HttpChannelFactory1.OnCreateChannelCoreEndpointAddress远程地址,Uri通过System.ServiceModel.Channels.ChannelFactoryBase1.InternalCreateChannelEndpointAddress地址,Uri通过。。。。