powershell - X509Store构造函数(字符串)的意义是什么?

标签 powershell certificate x509

似乎可以基于任何字符串设置有效的X509Store对象。
例如。

$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("abcdef")

我原来是用
 $store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Cert:\CurrentUser\My")

以为我对My存储有一个有效的对象,但是在调用时,我不断收到异常:
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::MaxAllowed) #Exception calling "Open" with "1" argument(s): "The parameter is incorrect.

字符串是否应采用某种格式?

编辑:
只要没有斜杠,字符串似乎就可以是任何东西。所以我需要使用$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My")

最佳答案

在咨询MSDN X509Store Class documentation之后,这里是我对其理解的要点。

X509Store类有许多构造函数。定义该类的实例后,可以使用Open方法打开它。

如果实例指向有效StoreLocation中的有效StoreName,则Open方法将打开证书存储。如果StoreLocation是正确的,则Open方法还可以基于使用的标志[System.Security.Cryptography.X509Certificates.OpenFlags]创建新商店。

如果未正确定义商店实例,则open方法将生成System.ArgumentException。

有效的StoreLocation值为

  • CurrentUser
  • LocalMachine

  • 有效的StoreName值是
  • 地址簿
  • AuthRoot
  • 证书颁发机构
  • 禁止使用
  • 我的
  • Trusted人
  • TrustedPublisher。

  • 这就是MSDN关于(String)构造函数的内容。

    “使用该构造函数为当前用户的特定X.509商店名称创建X509Store对象。要创建新商店,请指定一个不存在的名称。将使用该名称创建一个新商店。”

    因此,此代码应在“CurrentUser”中创建一个名为“abcdef”的新证书存储。
    $store = New-Object System.Security.Cryptography.X509Certificates.X509Store("abcdef")
    $openFlags = [System.Security.Cryptography.X509Certificates.OpenFlags]::MaxAllowed
    $store.Open($openFlags)
    

    可以使用MMC进行验证。

    因此,总而言之,商店构造函数参数“StoreName”和“String”是可以互换的。语义上,“StoreName”用于引用预定义值,“String”可以引用任何值。

    关于powershell - X509Store构造函数(字符串)的意义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115106/

    相关文章:

    c# - 如何将 SSL 证书从 PEM 转换为 DER 并保留私钥?

    powershell - 将字符串行放入 "array"

    PowerShell 动态数组

    php - PHP 证书。两种方式的身份验证 ssl。 Apache

    java - 无法从客户端 x509 证书中检索主体

    ruby - OpenSSL 没有足够的数据

    powershell - 如何在 Windows PowerShell 中进行屏幕截图?

    通过网络解锁 Windows session

    证书主题 X.509

    azure - SQL Azure 数据库的 CA 证书路径是什么?