[英]CORS customheaders is not working 405 Method Not Allowed
有一個用C#編寫的WCF Web服務,帶有兩個端點(SOAP + Rest)。 兩種服務都需要一個名為“ token”的自定義標頭字段。 此自定義標頭無法通過javascript代碼發送。 響應為405方法不允許 。 這是跨平台來源問題的錯誤。
我已經在尋找一些解決方案,但是沒有任何效果。 我嘗試通過在本地主機環境中嘗試一些代碼片段來解決此問題。
這是我的本地主機webservice的web.config:
<?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>
這是javascript代碼:
<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>
請求服務后,在響應中獲得我的自定義標頭(令牌,ApplicationPriviliges):
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
我發現最好的解決方案是在Web服務器的配置中設置customHeaders。 但是我不為我工作。
有人知道嗎?
非常感謝你!
您收到不允許的405方法,因為您的請求中包含導致預檢請求的標頭Token。 印前檢查請求使用OPTIONS方法,並且您的服務顯然不允許使用此方法。
將customHeaders添加到web.config不足以使Preflight請求正常工作。 服務器必須返回一個空的正文響應,並帶有一堆標題,告訴瀏覽器允許的內容。
幸運的是,IIS團隊已經發布了IIS CORS模塊,該模塊使您可以在web.config中以聲明方式配置CORS。 例如:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<cors enabled="true" failUnlistedOrigins="true">
<add origin="*" />
<add origin="https://*.microsoft.com"
allowCredentials="true"
maxAge="120">
<allowHeaders allowAllRequestedHeaders="true">
<add header="header1" />
<add header="header2" />
</allowHeaders>
<allowMethods>
<add method="DELETE" />
</allowMethods>
<exposeHeaders>
<add header="header1" />
<add header="header2" />
</exposeHeaders>
</add>
<add origin="http://*" allowed="false" />
</cors>
</system.webServer>
</configuration>
您可以在這里下載表格
這是文檔。
不幸的是,它用於完整的IIS,而不是IIS Express。 但是,您可以配置Visual Studio解決方案以與本地IIS一起使用。 您必須安裝它並以管理員身份執行Visual Studio,但是它可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.