以编程方式将证书添加到个人商店

时间:2014-06-27 17:20:41

标签: c# wcf certificate x509certificate wif

我正在处理的项目包括一个与WCF Web服务通信的MVC网站,通过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'的绑定中指定显式颁发者地址。或者必须在凭证中配置本地颁发者地址。

我认识到我与这些东西有很大的知识空间,所以我很感激一些指导!

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

1 个答案:

答案 0 :(得分:4)

这一行:

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