所以使用 aspnet_regiis.exe util 我做了以下
//Create the container
aspnet_regiis -pc MyRSAKey -exp
//Write key to file
aspnet_regiis -px MyRSAKey MyRSAKey.xml
//Install the key into a machine-level RSA key provider.
aspnet_regiis -pi MyRSAKey MyRSAKey.xml
//Grant access to the contrainer
aspnet_regiis -pa "MyRSAKey" "NT Authority\Network service"
现在我认为要使用此 key ,我需要将其添加到 web.config 文件中
<configProtectedData defaultProvider="MyProviderName">
<providers>
<add
name="MyProviderName"
type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
keyContainerName="MyRSAKey"
useMachineContainer="true" />
</providers>
现在,当我运行此命令时,它可以工作:
aspnet_regiis -pef "sectiomName" "pathToConfigFile" -prov "MyProviderName"
问题是,无论我对 keyContainerName 有什么值,它都可以工作。或者即使我完全从配置文件中取出 keyContainerName 它仍然有效,表明它实际上没有使用我生成和安装的 key 。
此外,visual studio 2010 甚至无法识别 keyContainerName(或 useMachineContainer),说不允许使用“keyContainerName”名称。
这里发生了什么?
最佳答案
乱序解决这两个问题:
Visual Studio 2010 doesn't even recognise
keyContainerName
(oruseMachineContainer
) saying that the 'keyContainerName' name is not allowed.What's going on here?
我没有反编译相关的配置节类来检查,但我观察到
RsaProtectedConfigurationProvider
有属性KeyContainerName
和 UseMachineContainer
,所以似乎是 a) 在解析 providers/add
时element 它使用反射在 type
的实例上设置相应的字段; b) 谁编写了 VS2010 用来验证的 XML 模式 .config
文件忘记了 <xsd:anyAttribute>
标签。(FWIW 这个问题正是我在发现您的问题时希望回答的问题,该问题在 Google 中排名靠前
keycontainername attribute is not allowed
)。The thing is that it works no matter what value I have for keyContainerName. Or even when I take keyContainerName out of the config file completely it still works suggesting that it's not actually using the key I generated and installed.
当你说“它有效”时,我想你的意思是
aspnet_regiis -pef
不会出错。但是,如果您尝试访问代码中 protected 配置部分,我敢打赌它会提示,除非您使用了正确的 keyContainerName
.我怀疑如果名称与已知的 key 容器不对应,它会创建一个新的,但我没有尝试验证这一点。
关于.net - 为什么加密 Web.config 文件可以在不提供 keyContainerName 的情况下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4934855/