MVa*_*afa 6 c# httpclient .net-core asp.net-core splunk-sdk
我在ASP.NET核心应用程序中使用了第三方库(Splunk c#SDK)。我正在尝试通过此SDK连接到我的localhost Splunk服务,但出现异常消息:
System.Net.Http.HttpRequestException:无法建立SSL连接,请参阅内部异常。
内部异常说:
根据验证过程,远程证书无效。
该SDK在后台使用HTTP客户端,但是我无权访问此对象来配置HttpClientHandler。
我在Google上进行的所有搜索最终都使用ServicePointManager绕过了SSL验证,但是该解决方案在Asp.Net核心中不起作用。
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Run Code Online (Sandbox Code Playgroud)
有什么方法可以绕过ASP.NET Core中的验证吗?
小智 58
当我在开发人员机器上使用身份服务器 (.net core) 和 Web api (.net core) 时,我意识到我需要信任 localhost 的 ssl 认证。该命令为我完成了这项工作:
dotnet dev-certs https --trust
Run Code Online (Sandbox Code Playgroud)
小智 24
如果您要添加 IHttpClient 并通过 DI 注入,您可以在类上添加配置Startup.cs。
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("yourServerName").ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }
});
}
Run Code Online (Sandbox Code Playgroud)
然后从依赖注入的类中调用它。
public class MyServiceClass
{
private readonly IHttpClientFactory _clientFactory;
public MyServiceClass (IConfiguration configuration, IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task<int> DoSomething()
{
var url = "yoururl.com";
var client = _clientFactory.CreateClient("yourServerName");
var result = await client.GetAsync(url);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 14
安装 .NET Core SDK 会将 ASP.NET Core HTTPS 开发证书安装到本地用户证书存储中。证书已安装,但不受信任。要信任该证书,请执行一次性步骤来运行 dotnet dev-certs 工具:
dotnet dev-certs https --trust
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请访问此链接
小智 9
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) =>
{
// local dev, just approve all certs
if (development) return true;
return errors == SslPolicyErrors.None ;
};
Run Code Online (Sandbox Code Playgroud)
这个博客帮助了我
https://www.khalidabuhakmeh.com/validate-ssl-certificate-with-servicepointmanager
这对我有用,
通过提供自定义HttpClientHandler创建Splunk.Client.Context,这将绕过SSL无效证书错误。
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
// Create Context
Context context = new Context(Scheme.Https, "localhost", 8089, default(TimeSpan), handler);
// Create Service
service = new Service(context);
Run Code Online (Sandbox Code Playgroud)
是的,您可以使用以下代码绕过证书...
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
// Pass the handler to httpclient(from you are calling api)
var client = new HttpClient(clientHandler)
Run Code Online (Sandbox Code Playgroud)
您收到此错误是因为您的应用程序无法验证连接的证书,并且将此错误用于创建会话/登录令牌的 API 尤为常见。您可以以危险的方式绕过它,如上所示,但显然这不是一个好的解决方案,除非您只是进行测试。
最好、最简单的解决方案是使用“ modernhttpclient-updated”Nuget 包,其代码在此 GitHub 存储库中共享,其中还有大量文档。
添加 Nuget 包后,将 NativeMessageHandler 传递到 HttpClient() 中,如图所示并构建:
var httpClient = new HttpClient(new NativeMessageHandler());
现在您会注意到您已经摆脱了该错误,并且会收到如下所示的不同错误消息Certificate pinning failure: chain error. ---> Javax.Net.Ssl.SSLPeerUnverifiedException: Hostname abcdef.ghij.kl.mn not verified: certificate: sha256/9+L...C4Dw=
要摆脱这个新的错误消息,您必须将错误中的主机名和证书密钥添加到 Pin 中,并将其添加到 NativeMessageHandler 的 TLSConfig 中,如下所示:
var pin = new Pin();
pin.Hostname = "abcdef.ghij.kl.mn";
pin.PublicKeys = new string[] { "sha256/9+L...C4Dw=" };
var config = new TLSConfig();
config.Pins = new List<Pin>();
config.Pins.Add(pin);
httpClient = new HttpClient(new NativeMessageHandler(true, config)
Run Code Online (Sandbox Code Playgroud)
请记住,您的其他(非令牌生成)API 调用可能不会实现证书固定,因此它们可能不需要此证书,并且它们经常可能使用不同的主机名。在这种情况下,您也需要将它们注册为引脚,或者只是为它们使用不同的 HttpClient!