在使用外部WCF服务时,在docker中运行的Dotnet核心web api无法进行身份验证

Lin*_*ahn 7 c# authentication wcf docker .net-core

我正在使用dotnet core 1.1.2构建RESTful API.

这个api的很大一部分需要向外部WCF服务发出请求.这些请求使用基于Windows的身份验证与用户名,密码和域进行身份验证.

我目前正在准备api生产,我想尝试将它停靠.

我遇到的问题是,一旦从docker容器中调用它,就会对第三方WCF服务进行身份验证失败.使用dotnet运行时运行API可以从windows和mac运行,并且服务将按照应有的方式进行身份验证.

我使用Visual Studio 2017的Connect wcf服务功能使用WCF服务,然后使用正确的身份验证模式修改端点绑定.

public ServiceSoapClient(EndpointConfiguration endpointConfiguration, string username, string password, string domain) :
base(ServiceSoapClient.GetBindingForEndpoint(endpointConfiguration), ServiceSoapClient.GetEndpointAddress(endpointConfiguration))
{
    this.ChannelFactory.Credentials.Windows.ClientCredential.UserName = username;
    this.ChannelFactory.Credentials.Windows.ClientCredential.Password = password;
    this.ChannelFactory.Credentials.Windows.ClientCredential.Domain = domain;

    this.Endpoint.Name = endpointConfiguration.ToString();
    ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.ServiceSoap))
    {
        System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
        result.MaxBufferSize = int.MaxValue;
        result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
        result.MaxReceivedMessageSize = int.MaxValue;
        result.AllowCookies = true;
        result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
        result.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
        return result;
    }
    throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过Ntml和Windows作为ClientCredentialType.

我已经验证,通过对应用程序内部的凭据进行硬编码,然后使用普通的dotnet运行时运行它以验证它是否有效,将api传输到docker容器时,身份验证凭据不会搞砸.最后使用完全相同的已发布应用程序构建docker镜像并再次运行它.当在docker中运行完全相同的应用程序时,它无法进行身份验证.

该应用程序的输出是:

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

这与我使用不正确的凭据时的输出相同.

我想知道这是否可能与网络如何与docker工作有关,以及api是否无法与WCF服务协商,因为它通过docker主机进行桥接.

如果任何对dotnet核心内部的Docker或WCF消费更有了解的人可能会有一些见解,那将非常有帮助.

最诚挚的问候,Linus.

Lin*_*ahn 2

对于遇到相同问题的任何人,这是由于非 Windows 平台上配置 kerberos 的方式造成的。这与 docker 无关,而是作为基于 Linux 的容器运行。

解决方案是将平台切换到 Windows 或在平台上正确配置 kerberos 身份验证。以下 github 问题对此进行了更详细的讨论:

https://github.com/dotnet/wcf/issues/2641https://github.com/dotnet/corefx/issues/9533