title: How do you pass user credentials from WebClient to a WCF REST service?
body: <p>I am trying to expose a WCT REST service and only users with valid username and password would be able to access it. The username and password are stored in a SQL database. </p>
  
  <p>Here is the service contract:</p>
  
  <pre><code>public interface IDataService
  {
      [OperationContract]
      [WebGet(ResponseFormat = WebMessageFormat.Json)]
      byte[] GetData(double startTime, double endTime);
  }
  </code></pre>
  
  <p>Here is the WCF configuration:</p>
  
  <pre><code>&lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="SecureBinding"&gt;
        &lt;security mode="Transport"&gt;
          &lt;transport clientCredentialType="Basic"/&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="DataServiceBehavior"&gt;
        &lt;serviceMetadata httpGetEnabled="true"/&gt;
        &lt;serviceCredentials&gt;
          &lt;userNameAuthentication
             userNamePasswordValidationMode="Custom"
             customUserNamePasswordValidatorType=
                   "CustomValidator, WCFHost" /&gt;
        &lt;/serviceCredentials&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;services&gt;
    &lt;service behaviorConfiguration="DataServiceBehavior" name="DataService"&gt;
      &lt;endpoint address="" binding="webHttpBinding"
                bindingConfiguration="SecureBinding" contract="IDataService" /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p>I am accessing the service via the WebClient class within a Silverlight application. However, I have not been able to figure out how to pass the user credentials to the service. I tried various values for client.Credentials but none of them seems to trigger the code in my custom validator. I am getting the following error: The underlying connection was closed: An unexpected error occurred on a send.</p>
  
  <p>Here is some sample code I have tried:</p>
  
  <pre><code>   WebClient client = new WebClient();
     client.Credentials = new NetworkCredential("name", "password", "domain");
     client.OpenReadCompleted += new OpenReadCompletedEventHandler(GetData);
     client.OpenReadAsync(new Uri(uriString));
  </code></pre>
  
  <p>If I set the security mode to None, the whole thing works. I also tried other clientCredentialType values and none of them worked. I also self-hosted the WCF service to eliminate the issues related to IIS trying to authenticate a user before the service gets a chance.</p>
  
  <p>Any comment on what the underlying issues may be would be much appreciated. Thanks.</p>
  
  <p><strong>Update</strong>: Thanks to Mehmet's excellent suggestions. Here is the tracing configuration I had:</p>
  
  <pre><code> &lt;system.diagnostics&gt;
      &lt;sources&gt;
        &lt;source name="System.ServiceModel"
                switchValue="Information, ActivityTracing"
                propagateActivity="true"&gt;
          &lt;listeners&gt;
            &lt;add name="xml" /&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
        &lt;source name="System.IdentityModel" switchValue="Information, 
                 ActivityTracing" propagateActivity="true"&gt;
          &lt;listeners&gt;
            &lt;add name="xml" /&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
      &lt;/sources&gt;
      &lt;sharedListeners&gt;
        &lt;add name="xml"
             type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="c:\Traces.svclog" /&gt;
      &lt;/sharedListeners&gt;
    &lt;/system.diagnostics&gt;
  </code></pre>
  
  <p>But I did not see any message coming from my Silverlight client. As for https vs http, I used https as follows:</p>
  
  <pre><code>string baseAddress = "https://localhost:6600/";
  _webServiceHost = new WebServiceHost(typeof(DataServices), 
                  new Uri(baseAddress));
  _webServiceHost.Open();
  </code></pre>
  
  <p>However, I did not configure any SSL certificate. Is this the problem?</p>