Encryption 加密web.config后在Visual Studio 2012中运行项目时出错

Encryption 加密web.config后在Visual Studio 2012中运行项目时出错,encryption,web-config,visual-studio-2012,Encryption,Web Config,Visual Studio 2012,我创建了一个自定义保护配置提供程序,以便使用X509Certificate加密projects web.config的两个部分,其中一个是自定义部分,另一个是ConnectionString部分。除了一个恼人的问题外,一切都很好 每次在Visual Studio 2012中运行应用程序时,我都会收到以下错误消息: 从配置文件获取连接字符串信息时发生以下错误。 加载配置时出错:无法加载文件或程序集FirstTitle.FastTitle.Security.X509ProtectedConfigur

我创建了一个自定义保护配置提供程序,以便使用X509Certificate加密projects web.config的两个部分,其中一个是自定义部分,另一个是ConnectionString部分。除了一个恼人的问题外,一切都很好

每次在Visual Studio 2012中运行应用程序时,我都会收到以下错误消息:

从配置文件获取连接字符串信息时发生以下错误。 加载配置时出错:无法加载文件或程序集FirstTitle.FastTitle.Security.X509ProtectedConfigurationProvider,版本=1.0.0.0,区域性=neutral,PublicKeyToken=D9B660F7F53533A

然而,该站点随后运行完全正常,自定义配置提供程序按预期实例化并成功解密web.Config

如果我将包含提供程序的程序集安装到GAC中,那么消息不会出现。但是,考虑到有许多开发人员在这个项目上工作,我真的不想让他们在运行项目之前将程序集安装到GAC中

此外,我不认为它应该安装到GAC中,因为它将只在这一个项目中使用

进一步资料:

我的配置提供程序位于其自己的项目FirstTitle.FastTitle.Security.X509ProtectedConfigurationProvider中,该项目已签名

提供程序的代码如下(该接口只是一个自定义接口):

web.config中的configProtectedData元素设置如下(出于安全原因,我删除了证书的名称):


正如我所说,除了VS2012中出现的这条恼人的消息之外,一切都正常。我一直在谷歌上搜索,试图找到答案,但没有结果。有人能帮忙吗

public class X509ProtectedConfigurationProvider : ProtectedConfigurationProvider,   IX509ProtectedConfigurationProvider
{
    #region Private Fields

    private const string _certNameConfigEntry = "CertSubjectDistinguishedName";

    private X509Certificate2 certificate = null;

    #endregion

    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        base.Initialize(name, config);

        // Get the CN for the cert from the config.
        string certSubjectDistinguishedName = config[_certNameConfigEntry];

        // Get the certificate form the local cert store.
        X509Store certStore = new X509Store(StoreLocation.LocalMachine);

        try
        {
            certStore.Open(OpenFlags.ReadOnly);

            // Find the matching certificate
            X509Certificate2Collection certs = certStore.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, certSubjectDistinguishedName, true);

            if (certs.Count > 0)
            {
                certificate = certs[0];
            }
            else
            {
                certificate = null;
            }
        }
        finally            
        {
            certStore.Close();
        }
    }

    public override System.Xml.XmlNode Decrypt(System.Xml.XmlNode encryptedNode)
    {
        // Load the config unto an XML document.
        XmlDocument doc = encryptedNode.OwnerDocument;
        EncryptedXml eXml = new EncryptedXml(doc);
        eXml.DecryptDocument();
        return doc.DocumentElement;
    }


    public override System.Xml.XmlNode Encrypt(System.Xml.XmlNode node)
    {
        // Load the config into an XML document.
        XmlDocument doc = new XmlDocument();
        doc.PreserveWhitespace = true;
        doc.LoadXml(node.OuterXml);

        EncryptedXml eXml = new EncryptedXml();
        EncryptedData eData = eXml.Encrypt(doc.DocumentElement, certificate);
        return eData.GetXml();
    }
}
<configProtectedData>
  <providers>
    <add name="X509Provider" type="FirstTitle.FastTitle.Security.X509ProtectedConfigurationProvider, FirstTitle.FastTitle.Security.X509ProtectedConfigurationProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9b660f7f535363a, processorArchitecture=MSIL"  CertSubjectDistinguishedName="CN=NameRemoved"></add>
  </providers>
</configProtectedData>