簡體   English   中英

如何在 Azure AppService 中將該根證書作為受信任的根導入?

[英]How to import that root certificate as trusted root in Azure AppService?

目前我正在做一個項目,我需要驗證證書的.cer版本。對於每筆交易,用戶將上傳一個新的.cer文件。 下面是我的代碼

       public static bool IsValidCert(this MemoryStream cer)
        {
            using X509Certificate2 cert = new X509Certificate2(cer.ToArray());
            return cert.Verify();
        }

所以在本地系統中,證書得到正確驗證(當前使用自簽名證書)。 因此,要確保將每個證書添加到使用X509Store代碼下面的根目錄中

public static bool IsValidCert(this MemoryStream cer)
        {

            using X509Certificate2 cert = new X509Certificate2(cer.ToArray());

            using X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);

            store.Open(OpenFlags.ReadWrite);
            store.Add(cert);
            store.Close();

            return cert.Verify();
        }

所以它適用於我的本地系統。 但是,當我將同一段代碼推送到Azure App service ,出現以下錯誤:- "Chain error: UntrustedRoot A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider." .

我沒有得到的問題在哪里。 在我的本地系統中,證書位於受信任的根證書頒發機構中。

如何將該根證書作為 Azure 中的受信任根導入?

用例:-我正在使用 GraphAPI 並且我正在為 AAD 應用程序創建客戶端證書。 所以用戶(已授權)將上傳證書以創建 AAD 應用程序。 所以我需要在將它推送到 Graph API 之前驗證證書是否有效。 目前使用X509Certificate2來驗證證書。 所以不受信任的證書(自簽名證書)在應用程序服務中阻止了我。

應用服務前端不對傳入的客戶端證書進行任何證書驗證。

對於應用服務多租戶 PaaS,不支持添加/刪除機器的受信任證書存儲。 目前僅 ASE(應用服務環境)支持添加根 CAS。 ASE -private-client-certificate

應用服務是在 Azure PaaS(平台即服務)基礎結構之上運行的服務。 應用服務在沙盒環境中運行,對底層機器有限制。

在應用服務中,請求的 TLS 終止發生在前端負載均衡器上。 在啟用客戶端證書的情況下將請求轉發到應用代碼時,應用服務會注入帶有客戶端證書的X-ARR-ClientCert請求標頭。 除了將其轉發到您的應用程序之外,應用服務不會對此客戶端證書執行任何操作。 您的應用程序代碼負責驗證客戶端證書。

另外,請在此處查看示例和文檔。

現在似乎沒有辦法在 AppService 中將它添加到 root(除了一些默認的由 MS 添加的)可以通過 Kudu 中的以下命令進行檢查。

dir cert:\localmachine\root

所以要處理這種情況,就采用了這種方式。 在使用X509Chain ,它將驗證所有證書驗證以及UntrustedRoot 所以我們需要為這個子案例添加一些額外的驗證(例如檢查哪些固定域列表)或需要允許UntrustedRoot證書。 代碼看起來像這樣

public static bool IsValidCert(this byte[] cer)
     {

            using X509Certificate2 cert = new X509Certificate2(cer.ToArray());
            using X509Chain certChain = new X509Chain();
            certChain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
            var result = certChain.Build(cert);


            if (!result && certChain.ChainStatus.Any(status => status.Status != X509ChainStatusFlags.UntrustedRoot))
            {
                foreach (X509ChainStatus chainStatus in certChain.ChainStatus)
                {
                    if (chainStatus.Status == X509ChainStatusFlags.UntrustedRoot)
                    {
                      //Custom logic to handle the scenario  
                    }
                    else
                    {
                      //throw exception for the validation error
                    }
                }
                throw new exception();
            }
            return true;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM