Asp.net mvc 4 MVC API CORS重复访问控制允许原始标头
我确实在互联网上到处搜索,但在尝试了几个小时后,我仍然有这个问题,我找到了每一个解决方案,但迄今为止没有成功。所以我做了一点MVCWebAPI来让CORS工作,但我总是对飞行前的请求有问题。api始终返回2个访问控制允许源标题Asp.net mvc 4 MVC API CORS重复访问控制允许原始标头,asp.net-mvc-4,asp.net-web-api,cors,Asp.net Mvc 4,Asp.net Web Api,Cors,我确实在互联网上到处搜索,但在尝试了几个小时后,我仍然有这个问题,我找到了每一个解决方案,但迄今为止没有成功。所以我做了一点MVCWebAPI来让CORS工作,但我总是对飞行前的请求有问题。api始终返回2个访问控制允许源标题 HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/8.5 Access-Control-Allow-Origin: * Access-Con
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: content-type
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: *
Date: Mon, 11 Jan 2016 11:06:56 GMT
Content-Length: 0
到目前为止我们都试过了
我安装了Microsoft.AspNet.WebApi.Cors,然后尝试了Microsoft.Owin.Cors
这是我使用的代码
WebApiConfig:
Public Sub Register(config As HttpConfiguration)
' Web API configuration and services
' Configure Web API to use only bearer token authentication.
'config.EnableCors(New EnableCorsAttribute("*", "*", "*"))
config.EnableCors()
'config.SuppressDefaultHostAuthentication()
'config.Filters.Add(New HostAuthenticationFilter(OAuthDefaults.AuthenticationType))
' Web API routes
'config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute( _
name:="ActionApi", _
routeTemplate:="{controller}/{action}" _
)
End Sub
控制器:
Imports System.Net
Imports System.Web.Http
Namespace Controllers
<Cors.EnableCors("*", "*", "*")>
Public Class TestController
Inherits ApiController
<HttpGet>
<ActionName("GetAnswer")>
Public Function GetAnswer() As List(Of String)
Dim AlIST As New List(Of String)
AlIST.Add("Test")
AlIST.Add("1234")
AlIST.Add("TTest")
Return AlIST
End Function
<HttpPost>
<ActionName("WriteAnswer")>
Public Function WriteAnswer(<FromBody> aList As List(Of String)) As List(Of String)
For i = 0 To aList.Count - 1
Dim Num As Integer = i + 1
Dim aStr As String = aList(i)
aList(i) = aList(i) + Num.ToString()
Next
For Each HD As String In HttpContext.Current.Response.Headers.AllKeys
aList.Add(HD)
For Each Header As String In HttpContext.Current.Response.Headers.GetValues(HD)
Dim a As String = Header
aList.Add(a)
Next
Next
Return aList
End Function
End Class
End Namespace
Web.Config:
<handlers>
<!--<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />-->
</handlers>
我尝试停用一些激活其他代码的代码,但到目前为止没有任何效果,我在网上找不到任何明确的解决方案
谢谢我认为问题在于在代码中同时使用Owin和WebAPI包。如果您希望在代码中使用Owin,您可以在Register方法中注释掉以下行(来自WebApi.Cors)
config.EnableCors()
另外,检查Web.config文件,查看是否未指定任何自定义cors头,如下所示。如果是,则也将其注释掉
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
...
</customHeaders>
</httpProtocol>-->
...
-->
我认为您可能需要在EnableCorsAttribute中添加SupportsCredentials=true。你是如何通过认证的?我不熟悉OAuth.我还没有使用凭证功能。supportsCredentials如何避免发送两次标题?我稍后会尝试,并让您知道…我在代码中有一个EnableCors,在web.config文件中有一个条目,导致它被添加到响应头两次
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
...
</customHeaders>
</httpProtocol>-->