title: The remote server returned an error: (401) Unauthorized. wcf httpbinding basic
body: <p>I create a simple wcf service   [ServiceContract]</p>
  
  <pre><code> public interface IService1
      {
  
          [OperationContract]
          [WebInvoke(Method = "GET", UriTemplate = "/Data/{data}")]
  
          string GetData(string data);
  
      }
  </code></pre>
  
  <p>With this custom auth validation :</p>
  
  <pre><code>   public class userpass : UserNamePasswordValidator
          {
              public override void Validate(string userName, string password)
              {
                  if (string.Equals(userName, "1", StringComparison.OrdinalIgnoreCase)
                      &amp;&amp; password == "1")
                      return;
                  throw new SecurityTokenValidationException();
              }
          }
  </code></pre>
  
  <p>And this webconfig :</p>
  
  <pre><code> &lt;bindings &gt; 
  
        &lt;webHttpBinding&gt;
  
          &lt;binding&gt;
            &lt;security mode="Transport"&gt;
              &lt;transport clientCredentialType="Basic"/&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
  
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
  </code></pre>
  
  <p>And </p>
  
  <p>
          
        </p>
  
  <p>And the client code :</p>
  
  <pre><code> Uri reqUri = new Uri("https://union-pc58.union.com/Service1.svc/data/asdsad");
  
  
  
              WebRequest req = WebRequest.Create(reqUri);
  
              req.PreAuthenticate = true;
  
              NetworkCredential credential = new NetworkCredential("1", "1");
  
              req.Credentials = credential;
  
              WebResponse resp = req.GetResponse();
  
  
              DataContractSerializer data = new DataContractSerializer(typeof(string));
              var res = data.ReadObject(resp.GetResponseStream());
  
              Console.WriteLine(res);
  </code></pre>
  
  <p>But when i run the client code i get this error :</p>
  
  <pre><code>An unhandled exception of type 'System.Net.WebException' occurred in System.dll
  
  Additional information: The remote server returned an error: (401) Unauthorized.
  </code></pre>