2017-09-03 106 views
0

我無法加載x509證書。IdentityServer4:無法加載證書,無法加載證書

問題:找不到證書。我查過了,它就在那裏! :(


public static X509Certificate2 Get() 
    { 
     var assembly = typeof(Certificate).GetTypeInfo().Assembly; 
     var names = assembly.GetManifestResourceNames(); 

     /*********************************************************************************************** 
     * Please note that here we are using a local certificate only for testing purposes. In a 
     * real environment the certificate should be created and stored in a secure way. 
     **********************************************************************************************/ 
     using (var stream = assembly.GetManifestResourceStream("Identity.API.Certificate.idsrv3test.pfx")) 
     { 
      return new X509Certificate2(ReadStream(stream), "idsrv3test"); 
     } 
    } 

    private static byte[] ReadStream(Stream input) 
    { 
     byte[] buffer = new byte[16 * 1024]; 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      int read; 
      while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       ms.Write(buffer, 0, read); 
      } 
      return ms.ToArray(); 
     } 
    } 

參考https://github.com/dotnet-architecture/eShopOnContainers/blob/38ade408629a606bc63bb09ea1f4e54ca473e617/src/Services/Identity/Identity.API/Certificate/Certificate.cs

回答

1

我加載從Startup.cs與下面的代碼證書

X509Certificate2 cert = new X509Certificate2(Path.Combine(_env.ContentRootPath, "cetname.pfx"), "password"); 
+0

它工作嗎?你在哪裏放置證書?在根? – 001

+0

是的,在根文件夾中,我正在使用這樣的證書 –

1

您需要更改生成操作在.pfx文件中添加到EmbeddedResource。在Visual Studio中易於在文件的屬性窗口中進行操作。

你可以看到在這樣的csproj中的結果: https://github.com/dotnet-architecture/eShopOnContainers/blob/38ade408629a606bc63bb09ea1f4e54ca473e617/src/Services/Identity/Identity.API/Identity.API.csproj

+0

我試過了,它沒有用!所以我只是直接從文件中讀取,這工作,但現在我有一個不同的問題。 – 001