Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linux上的.NET核心X509Store_C#_.net_Linux_Ubuntu_Asp.net Core 2.0 - Fatal编程技术网

C# linux上的.NET核心X509Store

C# linux上的.NET核心X509Store,c#,.net,linux,ubuntu,asp.net-core-2.0,C#,.net,Linux,Ubuntu,Asp.net Core 2.0,当使用.NET Core 2X509Store时,linux中的证书文件位于哪里 在Windows上,可以从管理控制台certlm.msc或使用powershell中的新自签名证书访问证书。使用.NETAPI,可以在Windows和linux上通过类似的方式添加证书 using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser)) { store.Open(OpenFlags.ReadWrite);

当使用.NET Core 2
X509Store
时,linux中的证书文件位于哪里

在Windows上,可以从管理控制台
certlm.msc
或使用powershell中的
新自签名证书
访问证书。使用.NETAPI,可以在Windows和linux上通过类似的方式添加证书

using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
    store.Open(OpenFlags.ReadWrite);
    var cert = new X509Certificate2("cert.pfx", "1234");
    store.Add(cert);
}
可通过
X509Store.Certificates.Find()访问


但是这些文件存储在哪里?如何通过linux工具添加它们?e、 g.系统管理员将添加证书,而应用程序将只读取证书。

我在更新应用程序以使用ASP.NET Core 2.1时遇到类似问题。到数据库的SSL连接不再接受连接字符串中的PFX文件(CentOS,在Windows上工作),因此我必须将PEM证书文件添加到
/etc/pki/tls/certs
,将PEM密钥文件添加到
/etc/pki/tls/private


这阻止了X509Store.Open()引发异常。

~/.dotnet/corefx/cryptography/x509stores/

关于@mbican的答案是正确的。证书存放在

~/.dotnet/corefx/cryptography/x509stores/

我不相信这个没有上下文的一句话回答,也不理解他是如何做到的。这就是为什么我想分享我的烦恼,作为未来所有遇到同样问题的访客的答案

  • 使用pfx认证文件,您不必将其转换为pem或crt或其他文件

  • 使用dotnet存储证书,以便查看文件的放置位置。一个小小的C#命令行:

    using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadWrite))
    {
        store.Add(new X509Certificate2(
            "./thePathToTheCert.pfx", "passwordOfTheCert", 
            X509KeyStorageFlags.PersistKeySet));
    }
    
    这创建了文件夹~/.dotnet/corefx/cryptography/x509stores/,并将证书放入其中。
    ~/.dotnet/corefx/cryptography/x509stores/my/thumbprintofcertificate.pfx

    提示:我们以前在windows上使用
    StoreLocation.LocalMachine
    ,但在linux上运行时没有LocalMachine存储,因此我们切换到
    StoreLocation.CurrentUser
    。如果您尝试LocalMachine,将出现此错误:
    Unix LocalMachine x509存储对所有用户都是只读的。


  • 希望这对其他人有所帮助。

    需要知道的重要一点是,证书是未加密的,这意味着如果您将该目录复制到另一台服务器上,它就可以工作,但也有泄漏私钥的风险。从LocalMachine store读取在linux上有效吗?或者检索证书的唯一可能的方法是在linux上使用StoreLocation.CurrentUser吗?@Igor我现在没有验证它,但据我所知,linux上只有CurrentUser可用。本地机器不工作。