2017-08-22 7 views
2

ich einen einfachen WCF-Dienst erstellen [Service]Der Remote-Server hat einen Fehler zurückgegeben: (401) Nicht autorisiert. wcf httpbinding Grund

public interface IService1 
    { 

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

     string GetData(string data); 

    } 

Mit diesem kundenspezifischen Auth-Validierung:

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(); 
      } 
     } 

Und dieses webconfig:

<bindings > 

     <webHttpBinding> 

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

     </webHttpBinding> 
    </bindings> 

Und

Und der 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); 

Aber wenn ich den Client-Code ausführen bekomme ich diesen Fehler: Statt

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

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

Antwort

3

diese Berechtigung Option Versuchen:

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

es interne Fehler 500 –

+0

kehrte ich die Standardauthentifizierung verwenden, weil ich keine Lösung für meine Frage finden Sie hier: https: //stackoverflow.com/questions/45767892/customauthorizationpolicy-evaluate-method-never-feuert-in-wcf-webhttpbinding –

+0

Fehler 500 bedeutet normalerweise, dass etwas mit Ihrer API nicht funktioniert - https://Stackoverflow.com/a/18713917/6266562 –

0

Did Sie installieren eine Zertifizierung? Vielleicht könnte Ihnen das helfen LINK, LINK

Ich habe die gleichen Probleme, weil meine Web-Service konnte nicht die Zertifizierung für https finden. IST hier einen Frieden meiner 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> 
Verwandte Themen