[英]GetRequestContext().ClientCertificate always return null
[英]Request.HttpContext.Connection.ClientCertificate is always null
我在適用於 Linux 的 Azure 應用服務上部署了一個 ASP.Net 核心網站。
在控制器中,我正在嘗試獲取客戶端證書,如下所示:
var callerCertificate = Request.HttpContext.Connection.ClientCertificate;
我總是將callerCertificate
設為null 。 我試過await Request.HttpContext.Connection.GetClientCertificateAsync()
與相同的結果null 。
我的網站虛擬主機創建如下所示:
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseStartup<Startup>()
.UseSerilog();
我還為網站(在 Azure 中)設置了 SSL 設置,如下所示:
客戶端調用方是一個 net462 項目,它使用Microsoft.Rest.CertificateCredentials
將證書設置為 HTTP 請求。
var cred = new CertificateCredentials(_deviceCertificate)
...
await this.cred.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
您可以嘗試直接使用HttpClient
添加證書,而不是使用Microsoft.Rest.CertificateCredential
。
var clientHandler = new HttpClientHandler();
clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
clientHandler.ClientCertificates.Add(_deviceCertificate);
var client = new HttpClient(clientHandler);
var result = client.GetAsync("https://yourservice").GetAwaiter().GetResult();
您可能還需要配置 SSL 協議(SSL2、SSL3、TLS 等):
clientHandler.SslProtocols = SslProtocols.Tls;
回答我自己的問題:我能夠從標題中獲取客戶端證書
string clientCertFromHeader = Request.Headers["X-ARR-ClientCert"];
盡管如此,為什么 Request.HttpContext.Connection.ClientCertificate 沒有提供證書仍然是一個謎。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.