title: Can't configure credentials WCF with basic realm?
body: <p>I have added a Connected Reference in Visual Studio 2019. It consumed a https endpoint, and created all binding information needed into a <code>reference.cs</code> file.</p>
  <p>It didn't generate any <code>App.config</code> file, so I suspected what I needed was bundled into the <code>reference.cs</code> file. Indeed, looking into it, it mostly was.</p>
  <p>So I tried creating a client, specify client credentials in two ways, as you can see, but still, doesn't matter how I specify it, I get an exception when calling this code below.</p>
  <pre><code>        public async Task SendFile(Stream fileStream, string fileName, Guid machineKey)
          {
              _logger.LogInformation(&quot;Starting file sending to Manager 1.&quot;);
              _logger.LogInformation($&quot;Sending file {fileName} from Machine {machineKey}&quot;);
              try
              {
                  var client = new FileTransferClient(FileTransferClient.EndpointConfiguration.BasicHttpBinding_IFileTransfer, _options.FileTransferEndPoint)
                  {
                      ClientCredentials =
                      {
                          UserName =
                          {
                              UserName = _options.FileTransferUsername,
                              Password = _options.FileTransferPassword
                          }
                      }
                  };
  
                  client.ClientCredentials.UserName.UserName = _options.FileTransferUsername;
                  client.ClientCredentials.UserName.Password = _options.FileTransferPassword;
  
                  using (new OperationContextScope(client.InnerChannel))
                  {
  
                  }
  
                  await client.UploadAsync(new FileUploadMessage
                  {
                      // Assume that this is enough. Can't really supply file length...
                      FileInfo = new FileTransferInfo
                      {
                          TransferId = new Guid(),
                          MachineUUID = machineKey.ToString(),
                          Name = fileName
                      },
  
                      TransferStream = fileStream
                  });
              }
              catch (Exception e)
              {
                  _logger.LogError(&quot;An unexpected exception occurred while sending file to Manager 1G.&quot;, e);
              }
  
              _logger.LogInformation(&quot;File sending finished.&quot;);
          }
  </code></pre>
  <p>The exception is <code>&quot;The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic Realm'.&quot;</code></p>
  <p>I have compared to similar APIs that use the beforementioned <code>App.config</code>, and have edited the <code>reference.cs</code> to match the security I think it should have.</p>
  <p>Specifically, I've added the security related lines here:</p>
  <pre><code>       private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
         {
             if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IFileTransfer))
             {
                 System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                 result.MaxBufferSize = int.MaxValue;
                 result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                 result.MaxReceivedMessageSize = int.MaxValue;
                 result.AllowCookies = true;
                 result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
                 result.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                 result.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                 return result;
             }
             if ((endpointConfiguration == EndpointConfiguration.MetadataExchangeHttpsBinding_IFileTransfer))
             {
                 System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
                 System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
                 result.Elements.Add(textBindingElement);
                 System.ServiceModel.Channels.HttpsTransportBindingElement httpsBindingElement = new System.ServiceModel.Channels.HttpsTransportBindingElement();
                 httpsBindingElement.AllowCookies = true;
                 httpsBindingElement.MaxBufferSize = int.MaxValue;
                 httpsBindingElement.MaxReceivedMessageSize = int.MaxValue;
                 result.Elements.Add(httpsBindingElement);
                 return result;
             }
             throw new System.InvalidOperationException(string.Format(&quot;Could not find endpoint with name \'{0}\'.&quot;, endpointConfiguration));
         }
  </code></pre>
  <p>What I found dumbfounding, was that with embedding in the constructor calling setting the ClientCredentials, they were not in any way populated when I inspected the <code>client</code> with a debug session attached. Hence I tried to set it afterwards specifically.</p>
  <p>But either way, the end result is the same, get the same error.</p>
  <p>How can I resolve that error in Code?</p>
  <p>I can in theory try to add an <code>App.config</code> and do it there, but I don't know the Contract. And I am not sure what to look for in the generated <code>reference.cs</code> to identify it. So I'd prefer to learn to do this by Code, as the Contract is already in place there, and I can supply the endpoint via the <code>_options</code>, so it should be able to configure for different environments by that.</p>