.Net Core 2.0 调用 WCF 客户端配置 [英] .Net Core 2.0 call to WCF client configuration

查看:31
本文介绍了.Net Core 2.0 调用 WCF 客户端配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .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 凭据 = CredentialCache.DefaultNetworkCredentials;client.ClientCredentials.Windows.ClientCredential = 凭据;client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;var response = client.GetStuff("param").Result;

我面临许多问题:

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

我得到的当前错误如下:

HTTP 请求未经授权,客户端身份验证方案为匿名".从服务器收到的身份验证标头是协商,NTLM"

还有 ConnectedService.json(由 WCF Web 服务引用提供程序自动创建)有一个预定义的端点 Uri .. 我不明白为什么我需要手动将地址传递给客户端(代码似乎强迫我这样做).. 理想情况下,我想根据环境在 json 中动态修改它.

谢谢.

解决方案

我注意到您将当前登录用户作为 Windows 凭据传递(这也是启用模拟所必需的),但您没有明确设置客户端传输层安全凭据.

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

<块引用>

还有 ConnectedService.json(由 WCF Web 自动创建)Service Reference Provider) 有一个预定义的端点 Uri .. 我没有理解为什么我需要手动将地址传递给客户端(代码似乎在强迫我这样做)

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

私有静态 System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration){if ((endpointConfiguration == EndpointConfiguration.WebService1Soap)){System.ServiceModel.BasicHttpBinding 结果 = new System.ServiceModel.BasicHttpBinding();result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;结果.MaxBufferSize = int.MaxValue;result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;result.MaxReceivedMessageSize = int.MaxValue;结果.AllowCookies = true;返回结果;}

修改端点.

 私有静态 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 = "管理员";client.ClientCredentials.Windows.ClientCredential.Password = "123456";

如果有什么我可以帮忙的,请随时告诉我.

I have a .NET Core 2.0 application and need to call a WCF client from one of its controllers, and pass the user credentials for authentication.

Within the .net core app I created a reference for the WCF client using the Connected Services (WCF Web Service Reference Provider) and now in a process of configuring the call. Note that I can use the same endpoint form a 4.6 framework application without any problems.

Here's my code:

        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;

I face a number of problems:

It has to be a https call
I need to pass the currently log in user credentials to the call

The current error I get is as follows:

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

Also the ConnectedService.json (created automativcally by WCF Web Service Reference Provider) has a predefined endpoint Uri.. I don't understand why I need to pass the address to the client manually (the code seems to be forcing me to do so).. ideally I'd like to get this dynamically amended in json depending on environment.

Thanks.

解决方案

I noticed that you passed the current logged-in user as a Windows credential (which is also necessary for enabling impersonation), but you did not explicitly set the client credentials for the transport layer security.

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

Also the ConnectedService.json (created automativcally by WCF Web Service Reference Provider) has a predefined endpoint Uri.. I don't understand why I need to pass the address to the client manually (the code seems to be forcing me to do so)

You can modify the method of automatic generation of proxy client to construct client proxy class (located in the reference.cs)
Modify the binding security

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

Modify the endpoint.

  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");

Construct the client proxy class.

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

Feel free to let me know if there is anything I can help with.

这篇关于.Net Core 2.0 调用 WCF 客户端配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆