Object already exists when trying to use aspnet_regiis to encrypt web.config connectionStrings section

Issue:

When I tried to encrypt my web.config connectionStrings section with aspnet_regiis, I got an error message saying Object already exists. What happen?

Administration utility to install and uninstall ASP.NET on the local machine.
Copyright (C) Microsoft Corporation.  All rights reserved.
Encrypting configuration section...
An error occurred executing the configuration section handler for connectionStrings.


Failed to encrypt the section 'connectionStrings' using provider 'MyIisRsaProv'. Error message from the provider: Object already exists.


Failed!

Environment:

Windows 10, .Net Framework v4.0.30319, PowerShell ISE or Command Prompt

Resolution:

In order to run aspnet_regiis to encrypt configuration section(s), you need to run PowerShell ISE or Command Prompt with Run as administrator

Step-by-step instructions:-

  1. Click on search on your Windows 10 machine and type powershell or command prompt

  2. Right click on Windows PowerShell ISE and select “Run as administrator”
  3. Enter your encryption command and run, ex., C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pef connectionStrings c:\temp\ -prov MyIisRsaProv
  4. You should get succeed result as below:

    Microsoft (R) ASP.NET RegIIS version 4.0.30319.0

    Administration utility to install and uninstall ASP.NET on the local machine.

    Copyright (C) Microsoft Corporation. All rights reserved.

    Encrypting configuration section...

    Succeeded!

Troubleshoot:

If you still having problem, make sure you run PowerShell or Command Prompt as Administrator by checking Window Title:
 

Reference:

Sample PowerShell to encrypt existing configuration sections:

        $app_path = "c:\your\Web.Config\folder"
        Write-Host "> connectionStrings"
        $bol_encrypt_config_here = 0
        Get-Content $app_path\web.config | ForEach-Object {if($_ -match "connectionStrings"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pef connectionStrings $app_path -prov MyIisRsaProv }
       
        Write-Host "> machineKey"
        $bol_encrypt_config_here = 0
        get-content $app_path\web.config | foreach-object {if($_ -match "machineKey"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { c:\windows\microsoft.net\framework\v4.0.30319\aspnet_regiis -pef system.web/machineKey $app_path -prov MyIisRsaProv }
       
        Write-Host "> sessionState"
        $bol_encrypt_config_here = 0
        get-content $app_path\web.config | foreach-object {if($_ -match "sessionState"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { c:\windows\microsoft.net\framework\v4.0.30319\aspnet_regiis -pef system.web/sessionState $app_path -prov MyIisRsaProv }
       
        Write-Host "> appSettings"
        $bol_encrypt_config_here = 0
        get-content $app_path\web.config | foreach-object {if($_ -match "appSettings"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { c:\windows\microsoft.net\framework\v4.0.30319\aspnet_regiis -pef appSettings $app_path -prov MyIisRsaProv }