.Net Core 2.0调用WCF客户端配置

时间:2019-03-13 11:40:08

标签: c# wcf .net-core

我有一个.NET Core 2.0应用程序,需要从其控制器之一调用WCF客户端,并传递用户凭据进行身份验证。

在.net核心应用程序中,我使用连接服务(WCF Web服务参考提供程序)为WCF客户端创建了参考,现在正在配置调用。请注意,我可以在4.6框架应用程序中使用相同的端点,而不会出现任何问题。

这是我的代码:

        var binding = new BasicHttpBinding {Security = {Mode = BasicHttpSecurityMode.Transport}};

        var address = new EndpointAddress("https://my-endpoint.asmx");

        var client  = new MyAppSoapClient(binding, address);

        var credentials = CredentialCache.DefaultNetworkCredentials; 

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

        var response = client.GetStuff("param").Result;

我遇到了许多问题:

必须是一个https呼叫
我需要将当前登录的用户凭据传递给呼叫

我得到的当前错误如下:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate, NTLM'

此外,ConnectedService.json(由WCF Web服务参考提供程序自动创建)具有预定义的端点Uri。我不明白为什么我需要手动将地址传递给客户端(代码似乎迫使我这样做。)理想情况下,我想根据环境在json中对其进行动态修改。

谢谢。

2 个答案:

答案 0 :(得分:0)

我的绑定缺少安全性Ntlm凭据类型(请参见下文)。

问题解决了。

    var binding = new BasicHttpBinding {Security = {Mode = BasicHttpSecurityMode.Transport,
        Transport = new HttpTransportSecurity(){ClientCredentialType = HttpClientCredentialType.Ntlm } }};

答案 1 :(得分:0)

我注意到您已将当前登录用户作为Windows凭据传递(这对于启用模拟也是必需的),但是您并未为传输层安全性明确设置客户端凭据。

BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  

ConnectedService.json(由WCF Web自动创建)   服务引用提供程序)具有预定义的端点Uri。   了解为什么我需要手动将地址传递给客户端(   代码似乎迫使我这样做)

您可以修改自动生成代理客户端的方法以构造客户端代理类(位于reference.cs中)
修改绑定安全性

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
            {
                System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
                result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.AllowCookies = true;
                return result;
            }

修改端点。

  private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
        {
            return new System.ServiceModel.EndpointAddress("http://10.157.13.69:8001/webservice1.asmx");

构造客户端代理类。

ServiceReference1.WebService1SoapClient client = new WebService1SoapClient(WebService1SoapClient.EndpointConfiguration.WebService1Soap);
            client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
            client.ClientCredentials.Windows.ClientCredential.Password = "123456";

请随时告诉我是否有什么可以帮忙的。