title: Setting credentials when using MetadataExchangeClient
body: <p>would appreciate some help with this - I feel I must be missing something obvious. I want to use the System.ServiceModel.Description.MetadataExchangeClient to retrieve information about a Web service. If the Web service is unsecured, then I have no problem, but if the Web service is a WCF service secured by Windows authentication, then I get an error along the lines of: Metadata contains a reference that cannot be resolved: 'http://server/Service.svc?WSDL'.</p>
  
  <p>My code is fairly simple:</p>
  
  <pre><code>Uri endpointUri = new Uri("http://server/Service.svc?WSDL");
  System.ServiceModel.Description.MetadataExchangeClient mexClient = new System.ServiceModel.Description.MetadataExchangeClient(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
  mexClient.ResolveMetadataReferences = true;
  System.Net.NetworkCredential mexNetworkCredentials = new System.Net.NetworkCredential(username, password, domain);
  mexClient.HttpCredentials = mexNetworkCredentials;
  System.ServiceModel.Description.MetadataSet metadataSet = mexClient.GetMetadata(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
  </code></pre>
  
  <p>Am I just missing something in the way that the client needs to specify authentication? I have googled around for a sample code, but nothing seems to match what I have in mind.</p>
  
  <p>Here is the Web.config file (slightly redacted):</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
      &lt;configSections&gt;
          &lt;sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
              &lt;sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
                  &lt;section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                  &lt;sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
                      &lt;section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/&gt;
                      &lt;section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                      &lt;section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                      &lt;section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                  &lt;/sectionGroup&gt;
              &lt;/sectionGroup&gt;
          &lt;/sectionGroup&gt;
      &lt;/configSections&gt;
      &lt;connectionStrings&gt;
          &lt;remove name="db"/&gt;
          &lt;add name="db" connectionString="server=&amp;quot;server&amp;quot;;database=&amp;quot;database&amp;quot;;Integrated Security=SSPI;" providerName="System.Data.SqlClient="/&gt;
      &lt;/connectionStrings&gt;
      &lt;system.web&gt;
          &lt;!--
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        --&gt;
          &lt;compilation&gt;
              &lt;assemblies&gt;
                  &lt;add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt;
                  &lt;add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;/assemblies&gt;
          &lt;/compilation&gt;
      &lt;/system.web&gt;
      &lt;location path="." inheritInChildApplications="false"&gt;
          &lt;system.web&gt;
              &lt;authentication mode="Windows"/&gt;
              &lt;pages&gt;
                  &lt;controls&gt;
                      &lt;add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;/controls&gt;
              &lt;/pages&gt;
              &lt;httpHandlers&gt;
                  &lt;remove verb="*" path="*.asmx"/&gt;
                  &lt;add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/&gt;
              &lt;/httpHandlers&gt;
              &lt;httpModules&gt;
                  &lt;add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;/httpModules&gt;
          &lt;/system.web&gt;
          &lt;system.codedom&gt;
              &lt;compilers&gt;
                  &lt;compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
                      &lt;providerOption name="CompilerVersion" value="v3.5"/&gt;
                      &lt;providerOption name="WarnAsError" value="false"/&gt;
                  &lt;/compiler&gt;
              &lt;/compilers&gt;
          &lt;/system.codedom&gt;
          &lt;system.webServer&gt;
              &lt;validation validateIntegratedModeConfiguration="false"/&gt;
              &lt;modules&gt;
                  &lt;remove name="ScriptModule"/&gt;
                  &lt;add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;/modules&gt;
              &lt;handlers&gt;
                  &lt;remove name="WebServiceHandlerFactory-Integrated"/&gt;
                  &lt;remove name="ScriptHandlerFactory"/&gt;
                  &lt;remove name="ScriptHandlerFactoryAppServices"/&gt;
                  &lt;remove name="ScriptResource"/&gt;
                  &lt;add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;/handlers&gt;
          &lt;/system.webServer&gt;
          &lt;system.serviceModel&gt;
              &lt;bindings&gt;
                  &lt;basicHttpBinding&gt;
                      &lt;binding name="SecuredHttpBinding" maxReceivedMessageSize="52428800"&gt;
                          &lt;readerQuotas maxArrayLength="52428800"/&gt;
                          &lt;security mode="TransportCredentialOnly"&gt;
                              &lt;transport clientCredentialType="Ntlm"/&gt;
                          &lt;/security&gt;
                      &lt;/binding&gt;
                  &lt;/basicHttpBinding&gt;
              &lt;/bindings&gt;
              &lt;services&gt;
                  &lt;service behaviorConfiguration="APIServiceBehavior" name="APIService"&gt;
                      &lt;endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecuredHttpBinding" name="IAPIService" contract="IAPIService" bindingNamespace="http://www.general.com/general/api/v1"/&gt;
                  &lt;/service&gt;
                  &lt;service behaviorConfiguration="APIServiceBehavior" name="Com.IntApp.Walls.API.InternalAPIService"&gt;
                      &lt;endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecuredHttpBinding" name="IInternalAPIService" contract="IInternalAPIService" bindingNamespace="http://www.general.com/general/api/v1"/&gt;
                  &lt;/service&gt;
              &lt;/services&gt;
              &lt;behaviors&gt;
                  &lt;serviceBehaviors&gt;
                      &lt;behavior name="APIServiceBehavior"&gt;
                          &lt;serviceMetadata httpGetEnabled="true"/&gt;
                          &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
                      &lt;/behavior&gt;
                  &lt;/serviceBehaviors&gt;
              &lt;/behaviors&gt;
          &lt;/system.serviceModel&gt;
      &lt;/location&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Thanks for any help, Martin</p>