【问题标题】:Programmatically adding certificate to personal store以编程方式将证书添加到个人商店
【发布时间】:2014-08-18 21:35:04
【问题描述】:

我正在处理的项目由一个 MVC 网站组成,该网站与 WCF Web 服务通信,并通过 Windows 身份进行身份验证。我有一个身份委托证书,我正在尝试以编程方式添加。要手动执行此操作,我在 mmc 中打开证书管理单元,将 .pfx 文件导入 Personal 并输入密码。然后我必须单击管理私钥并允许访问 IIS_IUSRS。 为了复制这个过程,我想出了以下控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        var cert = new X509Certificate2("location.pfx", "password", X509KeyStorageFlags.MachineKeySet);
        AddCert(StoreName.My, StoreLocation.LocalMachine, cert);
        AddAccessToCertificate(cert, "IIS_IUSRS");
    }

    private static void AddCert(StoreName storeName, StoreLocation storeLocation, X509Certificate2 cert)
    {
        X509Store store = new X509Store(storeName, storeLocation);
        store.Open(OpenFlags.ReadWrite);
        store.Add(cert);
        store.Close();
    }

    private static void AddAccessToCertificate(X509Certificate2 cert, string user)
    {
        RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;

        if (rsa != null)
        {
            string keyfilepath =
                FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName);

            FileInfo file = new FileInfo(keyfilepath + "\\" +
                rsa.CspKeyContainerInfo.UniqueKeyContainerName);

            FileSecurity fs = file.GetAccessControl();

            NTAccount account = new NTAccount(user);
            fs.AddAccessRule(new FileSystemAccessRule(account,
            FileSystemRights.FullControl, AccessControlType.Allow));

            file.SetAccessControl(fs);
        }
    }
    private static string FindKeyLocation(string keyFileName)
    {
        string text1 =
        Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
        string text2 = text1 + @"\Microsoft\Crypto\RSA\MachineKeys";
        string[] textArray1 = Directory.GetFiles(text2, keyFileName);
        if (textArray1.Length > 0)
        {
            return text2;
        }
        string text3 =
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string text4 = text3 + @"\Microsoft\Crypto\RSA\";
        textArray1 = Directory.GetDirectories(text4);
        if (textArray1.Length > 0)
        {
            foreach (string text5 in textArray1)
            {
                textArray1 = Directory.GetFiles(text5, keyFileName);
                if (textArray1.Length != 0)
                {
                    return text5;
                }
            }
        }
        return "Private key exists but is not accessible";
    }
}

不幸的是,这给出了错误:

未指定安全令牌发行者的地址。必须在目标“https://service.svc”的绑定中指定显式颁发者地址,或者必须在凭据中配置本地颁发者地址。

我意识到我对这些东西有很大的知识差距,所以我很感激一些指导!

我的问题是,我的手动流程和自动化流程有什么区别?

【问题讨论】:

    标签: c# wcf certificate x509certificate wif


    【解决方案1】:

    这一行:

    var cert = new X509Certificate2("location.pfx", "password", X509KeyStorageFlags.MachineKeySet);

    应该是

    var cert = new X509Certificate2("location.pfx", "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

    丢失的是X509KeyStorageFlags.PersistKeySet

    我收到了一些helpful information on certificates from here

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多