【问题标题】:Installing a pfx file with C# seems different to manually installing it使用 C# 安装 pfx 文件似乎与手动安装不同
【发布时间】:2015-10-26 15:55:42
【问题描述】:

我有一个安装 pfx 文件的 c# 程序(代码如下)

X509Certificate2 cert;
        cert = new X509Certificate2(@"myCert.pfx", "password");

        if (cert != null)
        {
            var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadWrite);
            if (!store.Certificates.Contains(cert))
            {
                store.Add(cert);
            }
        }

当我在管理控制台中查看证书时,这似乎将证书添加到了正确的位置。但是,我在 c# 中有一个 websocket 服务器,它将从商店中挑选出来并将其用于与浏览器的 ssl 连接,但是由于没有对证书的身份验证,浏览器都失败了。

但是,如果我要手动安装它(单击证书)并将其安装到同一位置,一切正常。

注意:它是自签名的

我该如何解决这个问题

【问题讨论】:

    标签: c# websocket pfx


    【解决方案1】:

    如果证书是自签名的,您也必须将它用于包含连接到 websocket 的 javascript 代码的 HTTPS 页面,以便浏览器提示您接受证书。

    您可以使用以下示例检索证书:

    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    store.Open(OpenFlags.ReadOnly);
    var certificate = store.Certificates[1];
    store.Close();
    

    然后将它用于您的 WebSocket 服务器。我不知道您使用的是哪个 .NET WebSocket 服务器,但in WebSocketListener is done this way

    【讨论】:

    • 是的,我在 c# 服务器中使用了它,它可以很好地挑选出证书。 HTTPS 站点具有有效的受信任证书。从字面上看,如果我通过 c# 安装本地证书,它将无法工作。手动安装效果很好
    • 您是否尝试过使用StoreName.My, StoreLocation.LocalMachine 安装证书?
    • 我会试试的。我实际上正在使用你的 dll 的 vtortola :)
    • 它不会让我添加到那个位置。我尝试了currentUser 并安装了它,但在服务器中检索证书时找不到它。似乎只有在放入 StoreName.Root, StoreLocation.CurrentUser 时才能找到它,手动安装证书可以正常工作……真是太痛苦了。 p.s.在 websocket 方面做得很好
    • 谢谢!我明天会尝试重现这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    相关资源
    最近更新 更多