编程语言
首页 > 编程语言> > C#-以编程方式将证书添加到个人商店

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

作者:互联网

我正在处理的项目包括一个与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”的绑定中指定一个明确的发行者地址,或者必须在凭据中配置本地发行者地址.

我认识到我对这些东西有很大的知识鸿沟,因此,我希望获得一些指导!

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

解决方法:

这行:

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.

标签:certificate,x509certificate,wif,wcf,c
来源: https://codeday.me/bug/20191029/1959707.html