c# - Webapi http 调用导致 SSL 异常

标签 c# ssl asp.net-core refit

我正在使用一个名为 Refit 的库访问 RESTful 服务

public interface IApiClient
{
    [Get("/{Id}/shifttimes")]
    Task<ShiftTimesResponse> GetShiftTimes(int id);
}

我正在 DI 中初始化接口(interface):

builder.Register(ctx =>
        {
            var baseUri = new Uri(config.ApiUri);
            var client = RestService.For<IApiClient>(baseUri.OriginalString);
            return client;

        }).As<IApiClient>().InstancePerTenant();

然后在 myHandler 中使用:

public class ShiftTimesHandler
{
    private readonly IApiClient _apiClient;

    public ShiftTimesHandler(
            IApiClient apiClient)
    {
       _apiClient= apiClient;
    }

    public async Task Execute(Message message)
    {
       **Error on this line**
        var shiftTimesResponse = await _apiClient.GetShiftTimes(message.Id); 

        //Do something
    }
}

异常(exception)是:

The SSL connection could not be established, see inner exception.

内部异常是:

The remote certificate is invalid according to the validation procedure.: AuthenticationException

最佳答案

下面为我工作。但是,它不应该在生产环境中完成:

            var httpClientHandler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
            };

            var httpClient = new HttpClient(httpClientHandler)
            {
                BaseAddress = new Uri(config.ApiUri);
            };

            return RestService.For<IHoursApiClient>(httpClient);

关于c# - Webapi http 调用导致 SSL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803301/

相关文章:

c# - ASP.Net 应用程序中的音频/视频/文本聊天

c# - C# 中惯用的可选返回值

ssl - 有没有办法让一些 traefik 服务自己管理他们的 tls 证书?

c# - asp.net core 2.2升级并且类库中不提供HttpSessionState

c# - 如何将字符串解析为 BigInteger?

java - SSL 套接字连接 : CFNetwork SSLHandshake failed (-9824) connecting to Java SSLServerSocket

ssl - 将 IIS Express SSL 证书分配给麦粒肿会断开我其余 IIS 站点证书的连接

asp.net-core - 有没有办法在.net core类库中使用IHostingEnvironment?

entity-framework - IDomainService 中的 ABP EF Core 多个 DbContext 访问抛出事务错误

c# - 存储库不会使用 UnitOfWorks 保存的更改进行更新