I doesn't sound to me like you've done everything correctly. First of all, there's two issues here:
- Ensuring the
machineKey
is the same on both web servers.
- Ensuring the same RSA private key is installed in a key container on both servers so that the encrypted configuration can be decrypted by each server.
These are separate concerns: the machineKey isn't relevant for encrypting/decrypting the config section you want to protect.
So first of all the aspnet_regiis -pc
command is used to create a new RSA key container and the reason it's failing is that the container name you've specified already exists because it's the default. The keypair in this container is not exportable so you need to create a new key container and specify the -exp
switch to denote that the keypair is exportable.
aspnet_regiis -pc "MyDeploymentKeyContainer" -exp
Then export the key to a file, including the private key: the private key is used to decrypt the config section so the web server will need it.
aspnet_regiis -px "MyDeploymentKeyContainer" deploykey.xml -pri
Now add the config section to your web.config and save it.
<configProtectedData>
<providers>
<add keyContainerName="MyDeploymentKeyContainer"
useMachineContainer="true"
description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
name="DeploymentProvider"
type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</configProtectedData>
Then encrypt the web.config section specifying the provider name as shown above (here it is "DeploymentProvider")
aspnet_regiis -pef "connectionStrings" "C:WebAppLocationFolder" -prov "DeploymentProvider"
Now you need to deploy the app to both servers and import the RSA key container you exported to the file earlier. Copy the file up and on each server run:
aspnet_regiis -pi deploykey.xml
Once that's done delete the file from the server - you don't want it hanging about. Finally grant the user account for the app pool running your web app access to the key container on both web servers.
aspnet_regiis -pa "MyDeploymentKeyContainer" SomeDomainSomeAccount
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…