首页 > 解决方案 > 如何在 c# 中列出所有带有 oid 的 Microsoft CA 证书模板

问题描述

我正在尝试检索 Microsoft CA 中的所有可注册证书模板。我可以使用 powershell 命令“Get-CATemplate”来做到这一点。我想在 c# 中做到这一点而不使用 powershell 命令

标签: c#certificate

解决方案


Microsoft CA 中所有可注册证书模板的列表可以使用ICertRequest2COM 接口检索。GetCAProperty此接口中有一个可用的方法。

您必须添加对CertCliCOM 库的引用。

在此处输入图像描述

然后使用CCertRequest实现ICertRequest接口的类。该方法以以下格式返回字符串值:TemplateName1\nTemplateOID1\nTemplateName2\nTemplateOID2\....您可以使用 \n 拆分此字符串。在下面的示例中,我仅选择模板名称。

    private const int CR_PROP_TEMPLATES = 0x0000001D;
    private const int PROPTYPE_STRING = 0x00000004;
    public void GetTemplates(string CAServer)
    {
        try
        {
            var objCertRequest = new CCertRequest();
            Regex regex = new Regex(@"([A-Za-z]+)");
            string templateString = objCertRequest.GetCAProperty(CAServer, CR_PROP_TEMPLATES, 0, PROPTYPE_STRING, 0);
            string[] templates = Regex.Split(templateString, @"\n");

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }

    }

更多详情请GetCAProperty 点击这里


推荐阅读