Search code examples
c#.netvisual-studiowcfwcf-security

User Secret Error (WCF) : User secrets are not configured correctly in the web.config


I am trying to learn how to use User Secrets. In a previous post I accountered some errors while applying it on App Console. So I decided to use it on WCF project.

When I click on the project and choose User Secrets this error pops up :

User secrets are not configured correctly in the web.config file
check that a reference to UserSecretsConfigBuilder has been added to
the <configBuilder> <builder> section of web config

Here is a picture of the error :

enter image description here

Here is my web.config file :

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="configBuilders"
      type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
      restartOnExternalChanges="false" requirePermission="false"/>
  </configSections>
  <configBuilders>
    <builders>
    </builders>
  </configBuilders>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2"/>
    <httpRuntime targetFramework="4.7.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- Pour éviter la divulgation d'informations de métadonnées, définissez les valeurs ci-dessous sur false avant le déploiement -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- Pour recevoir les détails de l'exception dans les erreurs à des fins de débogage, définissez la valeur ci-dessous sur true. Pour éviter la divulgation d'informations d'exception, définissez-la sur false avant le déploiement -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        Pour parcourir le répertoire racine de l'application Web lors du débogage, définissez la valeur ci-dessous sur true.
        Définissez-la sur false avant le déploiement pour ne pas divulguer d'informations du dossier de l'application Web.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

Thank you very much in advance.


Solution

  • These 2 links explain how to add User Secrets in .NET 4.7 projects:

    TL;DR Add the following lines to your web.config:

    <configBuilders>
        <builders>
          <add name="Secrets" userSecretsFile="~/App_Data/secrets.xml" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </builders>
    </configBuilders>
    

    By using the userSecretsFile property you define the location for your secrets file, you can also use userSecretsId with a GUID value (or something else, doesn't have to be a GUID), then the project will create the secrets file in:

    C:\Users\[Username]\AppData\Roaming\Microsoft\UserSecrets\[YOUR GUID OR IDENTIFIER]
    

    If you use both properties, then the userSecretsFile property will get priority. In your project you can right-click 'Manage User Secrets' to easily edit your User Secrets.