【问题标题】:Setting machineKey in web.config not functioning as expected在 web.config 中设置 machineKey 未按预期运行
【发布时间】:2012-10-03 01:02:18
【问题描述】:

我有一个问题(或者至少我认为我有)。我正在尝试在其 web.config 中设置站点的机器密钥,以便为将来在站点之间共享表单身份验证数据做准备。我首先在web.config中设置如下:

  <machineKey validationKey="<SOME_VALUE>" decryptionKey="<SOME_VALUE>" validation="SHA1" decryption="AES"/>

作为测试,我编写了以下代码来测试 web.config 中的新 machineKey 元素:

var machineConfigMachineKey = (MachineKeySection)WebConfigurationManager.OpenMachineConfiguration().SectionGroups["system.web"].Sections["machineKey"];
var webConfigMachineKey = (MachineKeySection)WebConfigurationManager.OpenWebConfiguration("").SectionGroups["system.web"].Sections["machineKey"];
Response.Write("<pre>");
Response.Write("<b>machine.config decrypt:  </b>" + machineConfigMachineKey.DecryptionKey + "<br />");
Response.Write("<b>web.config decrypt:      </b>" + webConfigMachineKey.DecryptionKey + "<br />");
Response.Write("<br />");
Response.Write("<b>machine.config validate: </b>" + machineConfigMachineKey.ValidationKey + "<br />");
Response.Write("<b>web.config validate:     </b>" + webConfigMachineKey.ValidationKey + "<br />");
Response.Write("</pre>");
Response.End();

... 导致此显示:

machine.config decrypt:  AutoGenerate,IsolateApps
web.config decrypt:      AutoGenerate,IsolateApps

machine.config validate: AutoGenerate,IsolateApps
web.config validate:     AutoGenerate,IsolateApps

显然我对此感到非常困惑,因为我希望在 web.config 中看到来自新 machineKey 元素的自定义值,而不是“AutoGenerate,IsolateApps”。

我是否遗漏了一些对我来说应该非常明显的东西?

谢谢:)

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

    请改用静态 API WebConfigurationManager.GetSection("system.web/machineKey")。它会自动执行遍历配置层次结构的逻辑,找到最适用的一个(通常为当前应用程序的 ~/Web.config),并提取其特定值。

    【讨论】:

    • 甜蜜!下周我回到这个项目时会审查:)
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    相关资源
    最近更新 更多