简体   繁体   中英

The remote server returned an error: (401) Unauthorized. wcf httpbinding basic

I create a simple wcf service [ServiceContract]

 public interface IService1
    {

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/Data/{data}")]

        string GetData(string data);

    }

With this custom auth validation :

   public class userpass : UserNamePasswordValidator
        {
            public override void Validate(string userName, string password)
            {
                if (string.Equals(userName, "1", StringComparison.OrdinalIgnoreCase)
                    && password == "1")
                    return;
                throw new SecurityTokenValidationException();
            }
        }

And this webconfig :

 <bindings > 

      <webHttpBinding>

        <binding>
          <security mode="Transport">
            <transport clientCredentialType="Basic"/>
          </security>
        </binding>

      </webHttpBinding>
    </bindings>

And

And the client 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);

But when i run the client code i get this error :

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (401) Unauthorized.

Try this authorization option instead:

string credentials = "1:1";
req.Headers.Add(HttpRequestHeader.Authorization, "Basic "+ Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));

Did you install a certification? Maybe this could help you LINK , LINK

I got the same problems, because my web-service couldnt find the certification for https. Here ist a peace of my config:

<system.serviceModel>
    <services>
      <service name="MyApp.Service.ServiceControl.WCF.WcfService">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:9999/MyApp/Services" />
          </baseAddresses>
        </host>
        <endpoint address="https://localhost:9999/MyApp/Services" binding="basicHttpsBinding" contract="MyApp.Service.ServiceControl.WCF.IWcfService" bindingConfiguration="TransportSecurity">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpsBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </basicHttpsBinding>
    </bindings>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM