如何通过REST通信将客户端证书从.NET 5站点发送到.NET 4.7.2站点中的OwinRequest?

3phpmpom  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(111)

我正在尝试使用REST通信将客户端证书从.NET 5应用程序发送到FW 4.7.2应用程序
在.NET 5应用程序中,我使用HttpClientHandler从证书存储中添加客户端证书。这必须在FW 4.7.2应用程序中通过OwinRequest接收。
我们使用HttpClient并添加带证书的HttpClientHandler。

var handler = new HttpClientHandler();
// Add valid client certificate to handler
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.AddRange(GetClientCertificate(x509AuthSettings));
// Create new HTTP Client using handler
var client = new HttpClient(handler);

var json = JsonConvert.SerializeObject(xmlMessage);
LoggingHelper.WriteLogsForAllLoggers($"requestUri: {requestUri}", PriorityEnum.Info, new string[] { LoggingConstants.DELIVER_MESSAGE_LOGGER });
// Create POST request to serverUri endpoint
var request = new HttpRequestMessage
{
    RequestUri = new Uri(requestUri),
    Method = HttpMethod.Post,
    Content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json")
};

// Send request and wait for the response
LoggingHelper.WriteLogsForAllLoggers($"Send POST request to {requestUri}. Timeout is set to {client.Timeout}. Waiting for response...", PriorityEnum.Info, new string[] { LoggingConstants.DELIVER_MESSAGE_LOGGER });

var response = client.SendAsync(request).Result;

字符串
我的问题是当收到请求时,从客户端发出的证书不存在。我试图通过ssl.ClientCertificate键在上下文中查找。

IDictionary<string, object> owinEnvironment = Request.Environment;

var certLoader = Context.Get<Func<Task>>("ssl.LoadClientCertAsync");
if (certLoader != null)
{
    await certLoader();
}

X509Certificate2 clientCert = Context.Get<X509Certificate2>("ssl.ClientCertificate");

iq3niunx

iq3niunx1#

对于IIS,将站点的SSL设置更改为accept client certificates


的数据
对于IISExpress,编辑您的.vs/<SOLUTION_NAME>/config/applicationhost.configset this attribute to true

<iisClientCertificateMappingAuthentication enabled="true">
</iisClientCertificateMappingAuthentication>

字符串
我不需要对“ssl.LoadClientCertAsync”做任何事情,或者调用await certLoader();。当涉及到我的OAuthAuthorizationServerProvider.ValidateClientAuthentication方法时,上下文已经填充了OWIN环境的“ssl.ClientCertificate”中的证书。

相关问题