加密 Web.Config 中的连接字符串失败

发布于 2024-11-03 15:45:17 字数 869 浏览 1 评论 0原文

我想加密我的 web.config 的连接字符串。 在这里我找到了一个关于如何执行此操作的很好的示例。我实现了这个并在我的开发机器上运行 find 。 但是,如果我将其上传到提供程序,则它不起作用并出现以下错误:

[SecurityException:请求失败。] System.Configuration.DpapiProtectedConfigurationProvider.Encrypt(XmlNode节点)

这个我读过的博客,这是因为网络可能在中等信任度下运行,因此无法使用 WebConfigurationManager.OpenWebConfiguration 。相反,应该使用 WebConfigurationManager.GetSection。但是,如果我按照建议获取该部分,则对 ProtectSection 的调用将失败,并显示以下错误消息:

System.InvalidOperationException:此操作在运行时不适用

任何人都可以引导我吗?对于解决方案,我如何编码(和解码)web.config 文件中的连接字符串(在运行时)?

更新
这不是问题的真正答案,但托管商完全信任网络,现在一切正常。我将问题悬而未决,也许有人发布了原始问题的解决方案,并帮助有同样问题但没有得到充分信任的人。

I would like to encrypt the connection string of my web.config. Here I have found a nice example on how to do this. I implemented this and on my development machine this runs find.
However if I upload it to the provider, it does not work with the following error:

[SecurityException: Request failed.]
System.Configuration.DpapiProtectedConfigurationProvider.Encrypt(XmlNode node)

In this blog I have read, that this is because of the web probably runs in medium trust and therefore WebConfigurationManager.OpenWebConfiguration can not be used. Instead of this, WebConfigurationManager.GetSection should be used. However, if I get the section as proposed, the call to ProtectSection fails with the following error message:

System.InvalidOperationException: This operation does not apply at runtime

Can anyone lead me to a solution, how I can encode (and decode) the connection string in the web.config file (at runtime)?

Update
Not a real answer to the question, but the hoster gave full trust to the web and now, all worked fine. I leave the quesion open, maybe someone posts a solution to the original question and helps with this people having the same problem but not getting full trust.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2024-11-10 15:45:17

来自 http://msdn.microsoft.com/ en-us/library/89211k9b%28v=vs.80%29.aspx

static void ToggleWebEncrypt()
{
    // Open the Web.config file.
    Configuration config = WebConfigurationManager.
        OpenWebConfiguration("~");

    // Get the connectionStrings section.
    ConnectionStringsSection section =
        config.GetSection("connectionStrings")
        as ConnectionStringsSection;

    // Toggle encryption.
    if (section.SectionInformation.IsProtected)
    {
        section.SectionInformation.UnprotectSection();
    }
    else
    {
        section.SectionInformation.ProtectSection(
            "DataProtectionConfigurationProvider");
    }

    // Save changes to the Web.config file.
    config.Save();
}

更新

另外,请确保您的服务帐户具有 Web.config 的写入权限。另外,请注意,向 Web.config 上的服务帐户授予写入权限会在一定程度上增加应用程序的安全足迹。仅当您了解并接受风险时才这样做。

From http://msdn.microsoft.com/en-us/library/89211k9b%28v=vs.80%29.aspx

static void ToggleWebEncrypt()
{
    // Open the Web.config file.
    Configuration config = WebConfigurationManager.
        OpenWebConfiguration("~");

    // Get the connectionStrings section.
    ConnectionStringsSection section =
        config.GetSection("connectionStrings")
        as ConnectionStringsSection;

    // Toggle encryption.
    if (section.SectionInformation.IsProtected)
    {
        section.SectionInformation.UnprotectSection();
    }
    else
    {
        section.SectionInformation.ProtectSection(
            "DataProtectionConfigurationProvider");
    }

    // Save changes to the Web.config file.
    config.Save();
}

UPDATE

Also, ensure that your service account has write permissions to the Web.config. Also, be aware that granting write permissions to your service account on the Web.config increases somewhat the security footprint of your application. Only do so if you understand and accept the risks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文