簡體   English   中英

從用於Google API的Azure網站中的p12證書生成X509Certificate2時出現502錯誤

[英]502 error when generating X509Certificate2 from p12 certificate in Azure Websites for Google API

我正在使用GoogleJsonWebToken類生成一個訪問令牌,用於對Google Calendar API的json調用。 當我使用以下內容(使用我的實際服務帳戶電子郵件)時,它在我的開發機器上的IIS Express中運行得非常好:

string p12Path = HttpContext.Current.Server.MapPath("~/App_Data/certificate.p12");
var auth = GoogleJsonWebToken.GetAccessToken("uniquestring@developer.gserviceaccount.com",
                                             p12Path,
                                             "https://www.googleapis.com/auth/calendar");
string Token = auth["access_token"];

為了測試這個,我只是在我的cshtml razor視圖中調用@Token 當我將其發布到我的Azure網站時,它不起作用。 如果我保持GoogleJsonWebToken類未經修改,我得到一個非常無益的502 - Web server received an invalid response while acting as a gateway or proxy server. 沒有其他信息。

在一些谷歌搜索 ,我發現這個SO帖子是一個類似的問題。 所以我嘗試了他們的解決方案我得到System.Security.Cryptography.CryptographicException: The system cannot find the file specified. 何時從我的Azure網站運行。 當它從我的開發機器運行時,我得到System.Net.WebException: The remote server returned an error: (400) Bad Request. 我認為是因為使用該解決方案, CspKeyContainerInfo.KeyContainerNamenull而在我的開發機器上運行的原始未修改類給出了類似{C0E26DC5-5D2C-4C77-8E40-79560F519588} ,每次都是隨機生成的,這個值是用於簽名簽名的過程中。

然后我找到了這個SO帖子,但該解決方案產生的結果與上一個解決方案相同。

我也試過X509KeyStorageFlags大部分不同組合都無濟於事。

如何自己生成CspKeyContainerInfo.KeyContainerName或以其他方式成功生成X509Certificate2

我在這個MSDN論壇帖子上找到了解決方案。 基本上我需要設置X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet正如我在上面提到的第一篇SO帖子中讀到的那樣,然后我需要在我的CspParamaters中使用Flags = CspProviderFlags.UseMachineKeyStore CspProviderFlags.UseMachineKeyStore。

我已經在GitHub上發布了我的完整解決方案

首先,您應該將p12文件添加到資源:

右鍵單擊項目 - 屬性 - 資源 - 添加資源,然后使用此行代碼作為證書

var certificate = new X509Certificate2((byte[])Properties.Resources.ResourceManager.GetObject(ResourceName), "!password!", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

暫無
暫無

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

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