微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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));
}

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

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

该应用程序的输出是:

HTTP请求未经授权使用客户端身份验证方案“Negotiate”.从服务器收到的身份验证标头是“Negotiate,NTLM”.

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

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

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

最诚挚的问候,Linus.

最佳答案
对于遇到相同问题的任何人来说,这是由于在非Windows平台上配置kerberos的方式.这与docker无关,而是作为基于linux的容器运行.

解决方案是将平台切换到Windows或在平台上正确配置kerberos身份验证.这在以下github问题中有更详细的讨论:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐