sirius91
Goto Top

CORS customheaders - 405 Method Not Allowed

Es gibt einen WCF-Webservice in C# geschrieben mit zwei Endpunkten (SOAP + Rest). Beide Dienste benötigen ein benutzerdefiniertes Header-Feld mit dem Namen "Token". Dieser benutzerdefinierte Header kann nicht mit JavaScript-Code gesendet werden. Die Antwort lautet immer "405 Method Not Allowed".
Der Fehler ist zurückzuführen auf das "cross platform origin" Problem.

Ich habe bereits nach Lösungen gesucht aber leider funktioniert davon nichts. Meisten war die Lösung des Problems in der web.config - im Bereich httpProtocol - customheaders. Ich habe versucht, das Problem zu beheben, indem ich einige Code-Snipets in meiner Localhost-Umgebung ausprobiere.

Dies ist die web.config meines localhost webservice:

<?xml version="1.0"?>  
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />  
    <httpRuntime targetFramework="4.5.1"/>  
    <customErrors mode="Off"/>  
  </system.web>

  <system.runtime.caching>
    <memoryCache>
      <namedCaches>
        <add name="Default" physicalMemoryLimitPercentage="10" cacheMemoryLimitMegabytes="128" />  
      </namedCaches>
    </memoryCache>
  </system.runtime.caching>

  <system.serviceModel>

    <services>
      <service name="MyProject.TokenTestService.Service"   
               behaviorConfiguration="ServiceAuthBehaviorHttp"  
               >      
       <endpoint address=""  
                  binding="basicHttpBinding"  
                  bindingConfiguration="BasicHttpBindingLocalhost"  
                  contract="MyProject.TokenTestService.IService"  
                  />
        <endpoint address="rest"  
                  behaviorConfiguration="AjaxEnabledBehavior"  
                  binding="webHttpBinding"  
                  bindingConfiguration="webBindingLocalhost"  
                  contract="MyProject.TokenTestService.IService"  
                  />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
      </service>
    </services>

    <behaviors>

      <endpointBehaviors>
        <behavior name="AjaxEnabledBehavior">  
          <webHttp helpEnabled="true" />  
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />  
          <serviceDebug includeExceptionDetailInFaults="true"/>  
        </behavior>

        <behavior name="ServiceAuthBehaviorHttp">  
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
          <serviceDebug includeExceptionDetailInFaults="true" />  
          <TokenValidationServiceExtension ApplicationId="2" CachingTimeInSeconds="3600"  />  
        </behavior>
      </serviceBehaviors>

    </behaviors>

    <extensions>
      <behaviorExtensions>
        <add 
            name="TokenValidationServiceExtension"   
            type="MyProject.Authentication.Common.Extensions.TokenValidationServiceBehaviorExtension, MyProject.Authentication.Common" />  
      </behaviorExtensions>
    </extensions>

    <bindings>
      <basicHttpBinding>

            <binding name="BasicHttpBinding_IAuthServiceSoap">  
              <security mode="Transport" />  
            </binding>

        <binding name="BasicHttpBinding_IAuthServiceSoapLocalhost" />  

        <binding name="BasicHttpBindingLocalhost" />  

      </basicHttpBinding>

      <webHttpBinding>
        <binding name="webBindingLocalhost" crossDomainScriptAccessEnabled="true" />  
      </webHttpBinding>
    </bindings>

    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />  
    </protocolMapping>    

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>  
    <directoryBrowse enabled="true"/>  

    <httpProtocol>
      <customHeaders>
        <add name="Origin" value="localhost" />  
        <add name="Access-Control-Allow-Origin" value="*" />  
        <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />  
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Token, ApplicationPriviliges" />  
        <add name="Access-Control-Max-Age" value="1728000" />  
      </customHeaders>
    </httpProtocol>

  </system.webServer>

</configuration>

Hier der JavaScript code:
<script>
$.ajax({
  url: "http://localhost:14305/Service.svc/rest/GetDataAdmin",  
  type: "GET",  
  crossDomain: true,
  dataType: 'json',  
  data: null,
  headers: {
            'Content-Type':'application/json; charset=utf-8',  
            'Token':'3C6E27D9-ACA7-47D9-BB8B-1C960813D79C'  
  },
  success: function( result ) {
    $( "Result:" ).html( "<strong>" + result + "</strong>" );  
  },
  error: function( err ) {
    $( "Result:" ).html( "<strong>" + err + "</strong>" );  
  }
});
</script>

Nachdem der Webservice (Dienste) die Anfrage erhält, bekomme ich meine Auflistung der customHeader (token, ApplicationPriviliges) als Antwort:
Hier der Kopfbereich (Firefox Netzwerkanalyse):
Cache-Control: private
Allow: GET
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcVEZTXEdJVFxMaWZiaS5BcGkuQXV0aGVudGljYXRpb24uVGVzdFNlcnZpY2VcTGlmYmkuQXBpLlRva2VuVGVzdFNlcnZpY2VcU2VydmljZS5zdmNccmVzdFxHZXREYXRhQWRtaW4=?=
X-Powered-By: ASP.NET
Origin: localhost
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Token, ApplicationPriviliges
Access-Control-Max-Age: 1728000
Date: Mon, 05 Feb 2018 09:48:55 GMT
Content-Length: 1766

Die beste Lösung die ich gefunden habe war, die customHeaders in der Konfiguration des Webservers einzurichten.
Das habe ich wie oben beschrieben versucht, aber es funktoniert nicht.

Hat jemand eine Idee?

Vielen Dank!

Content-Key: 363740

Url: https://administrator.de/contentid/363740

Printed on: April 19, 2024 at 11:04 o'clock

Mitglied: 135333
135333 Feb 06, 2018 at 12:43:18 (UTC)
Goto Top