简体   繁体   English

CORS WCF:对预检请求的响应未通过访问控制

[英]CORS WCF: Response to preflight request doesn't pass access control

I have problem with WCF service access. WCF服务访问出现问题。

This is my WCF 这是我的WCF

[OperationContract]
    [WebInvoke(
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, Method = "POST",
        UriTemplate = "/GetAllRequest/")]    
    IEnumerable<USP_GET_DATA_Result> Get();

Then I following tutorial tutorial CORS 然后我跟随教程教程CORS

This is my service to get the data 这是我获取数据的服务

 function (app) { app.service('AngularService', ['$http', function ($http) { return { get: function () { return $http({ method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }, url: 'http://localhost:51458/ServiceRequest.svc/GetAllRequest/', data: {} }); } }; }]); 

Then this is my controller 那这是我的控制器

 (function (app) { 'use strict'; app.controller('entryCtrl', entryCtrl); entryCtrl.$inject = ['$scope', '$http', 'AngularService']; function entryCtrl($scope, $http, AngularService) { $scope.pageClass = 'page-entry'; //load data AngularService.get().success(function (response) { $scope.entryData = JSON.parse(response.d); }); } })(angular.module('entry')); 

Then when I run I have the error like: 然后,当我运行时,我会出现类似以下错误: 在此处输入图片说明

Edit: add my web.config 编辑:添加我的web.config

 <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <!--add element connectionStrings--> <connectionStrings> <add name="ConnectionString" connectionString="Data Source=192.168.0.202; Initial Catalog=example.KRIS; UID=sa; Password=*****" providerName="System.Data.sqlClient" /> <add name="Entities" connectionString="metadata=res://*/DBEntities.csdl|res://*/DBEntities.ssdl|res://*/DBEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.0.202;initial catalog=example.KRIS;user id=sa;password=******;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5.2" /> <httpRuntime targetFramework="4.5.2" /> </system.web> <system.serviceModel> <!--add element extensions and services--> <extensions> <behaviorExtensions> <add name="crossOriginResourceSharingBehavior" type="WcfService.CORSEnablingBehavior, WcfService, Version=1.0.0.0, Culture=neutral" /> </behaviorExtensions> </extensions> <services> <service behaviorConfiguration="serviceBehavior" name="WcfService.ServiceRequest"> <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService.IServiceRequest" /> </service> </services> <behaviors> <!--edit value--> <serviceBehaviors> <behavior name="serviceBehavior"> <serviceMetadata httpGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> <!--add element endpoint--> <endpointBehaviors> <behavior name="web"> <webHttp /> <crossOriginResourceSharingBehavior /> </behavior> </endpointBehaviors> </behaviors> <!--edit protocolmapping--> <protocolMapping> <add binding="webHttpBinding" scheme="http" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <!-- To browse web app root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web app folder information. --> <directoryBrowse enabled="true" /> </system.webServer> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration> 

Put following code in global.asax because you need to allow methods which can be called clients. 将以下代码放在global.asax因为您需要允许可以称为客户端的方法。

protected void Application_BeginRequest(object sender, EventArgs e)
{
   HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

   if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
   {
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
      HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
      HttpContext.Current.Response.End();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态 - CORS: Response to preflight request doesn't pass access control check: It does not have HTTP ok status “http://localhost:3000”已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查: - 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 对预检请求的响应未通过访问控制检查:否&#39;Access-Control-Allow-Origin&#39; - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' Angular 6 - 对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头 - Angular 6 - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header ASP.NET Core Web API + Angular 对预检请求的响应未通过访问控制检查:预检请求不允许重定向 - ASP.NET Core Web API + Angular Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request 对预检请求的响应未通过访问控制(Angular 2-Web Api) - Response to preflight request doesn't pass access control (Angular 2 - Web Api) 对预检请求的响应未通过访问控制检查(Angular2) - Response to preflight request doesn't pass access control check (Angular2) 对预检请求的响应未通过访问控制检查:响应中“ Access-Control-Allow-Credentials”标头的值为“ - Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' 在 ASP.NET 中,对预检请求的响应未通过访问控制检查:No &#39;Access-Control-Allow-Origin&#39; 标头 - In ASP.NET, Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header .NET 5 对预检请求的响应未通过访问控制检查:没有'Access-Control-Allow-Origin' header 存在于请求的资源上 - .NET 5 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM