我正在尝试将WCF api与.Net Core 2.1.2一起使用,但是我目前在认证验证方面遇到一些问题。
主要问题是,当我进行调试时,可以向服务器发出请求。当部署项目的可执行文件并在计算机中运行时,我都可以发出请求。但是,当我将同一可执行文件复制到接受环境时,代码将引发异常“无法为SSL / TLS安全通道建立信任关系”
我的机器不在接受环境之内(我正在使用VPN)。验收机在环境内部。
有什么想法吗?
谢谢!
private WSClient InstantiateProxy()
{
WSClient accessWSClient = new WSClient(EndpointConfiguration.MIAccessPort, Configuration["AppConfiguration:Endpoint"]);
accessWSClient.ClientCredentials.Windows.ClientCredential =
new NetworkCredential(Configuration["AppConfiguration:Username"], Configuration["AppConfiguration:Password"]);
ConfigureBinding(accessWSClient);
accessWSClient.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
{
CertificateValidationMode = X509CertificateValidationMode.None,
RevocationMode = X509RevocationMode.NoCheck,
};
return accessWSClient;
}
private static void ConfigureBinding(WSClient accessWSClient)
{
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding
{
MaxBufferSize = int.MaxValue,
ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max,
MaxReceivedMessageSize = int.MaxValue,
AllowCookies = true
};
binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
accessWSClient.Endpoint.Binding = binding;
}
答案 0 :(得分:1)
最近有同样的问题,这对我来说解决了(使用依赖注入)。 然后,只需在启动时调用AddWcfClient即可为每种环境注入正确的httpBinding。
我的情况是我在DEV中有http地址,在PROD中有https地址,所以这个人应该给您正确的httpBinding实例,以确保wcf是否为https。
public static class HttpBindingExtensions
{
public static BasicHttpBinding Https => new BasicHttpBinding
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
Security = new BasicHttpSecurity()
{
Mode = BasicHttpSecurityMode.Transport
}
};
public static BasicHttpBinding Http => new BasicHttpBinding
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue
};
public static IServiceCollection AddWcfClient<I, T>(this IServiceCollection services, string key)
where I : class
where T : class, I
=> services.AddScoped<I>(x => GetWcfInstance<I, T>(key, x));
private static T GetWcfInstance<I, T>(string key, IServiceProvider x) where I : class where T : class, I
{
var type = typeof(T);
var ctorInfo = type.GetConstructor(new[] { typeof(BasicHttpBinding), typeof(EndpointAddress) });
var config = (IConfiguration)x.GetService(typeof(IConfiguration));
var instance = (T)ctorInfo?.Invoke(new object[] { config.GetHttpBinding(key), config.GetEndpointAddress(key) });
return instance;
}
public static EndpointAddress GetEndpointAddress(this IConfiguration config, string key)
{
return new EndpointAddress(config[key]);
}
public static BasicHttpBinding GetHttpBinding(this IConfiguration config, string key)
{
return GetHttpBinding(config[key]);
}
public static BasicHttpBinding GetHttpBinding(string uri)
{
return uri.StartsWithIgnoreCase("https") ? Https : Http;
}
}