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

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

问题描述

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

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.

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

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.

这是我的代码:

        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呼叫
我需要将当前登录的用户凭据传递给呼叫

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'

此外,ConnectedService.json(由WCF Web服务参考提供程序自动创建)具有预定义的端点Uri.这样做)..理想情况下,我想根据环境在json中对其进行动态修改.

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.

谢谢.

推荐答案

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

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;

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

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)

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

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

修改端点.

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

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

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

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

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