【问题标题】:MVC API CORS Duplicate Access-Control-Allow-Origin headerMVC API CORS 重复的 Access-Control-Allow-Origin 标头
【发布时间】:2016-04-15 18:58:09
【问题描述】:

我确实在互联网上到处搜索,但在尝试了我找到的所有解决方案几个小时后,我仍然遇到这个问题,但到目前为止没有任何成功。所以我做了一个小的 MVC Web API 来让 CORS 工作,但我总是遇到预检请求的问题。 api 总是返回 2 个 Access-Control-Allow-Origin 标头。

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

启动:

Public Sub Configuration(app As IAppBuilder)
    ConfigureAuth(app)
    'app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll)
End Sub

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>

我尝试停用一些代码来激活其他代码,但到目前为止没有任何效果,我在网络上找不到任何明确的解决方案。

谢谢

【问题讨论】:

  • 我认为您可能需要将 SupportsCredentials = true 添加到您的 EnableCorsAttribute。你是如何传递凭证的?我不熟悉 OAuth。
  • 我还没有使用凭证功能。 supportCredentials 如何避免两次发送标头?稍后我会尝试并通知您...

标签: asp.net-mvc-4 asp.net-web-api cors


【解决方案1】:

我认为问题在于您的代码中同时使用了 Owin 和 WebAPI 包。如果您希望在代码中使用 Owin,您可以在 Register 方法(来自 WebApi.Cors)中注释掉以下行

config.EnableCors()

另外,检查您的 Web.config 文件,看看您是否没有指定任何自定义 cors 标头,如下所示。如果是,那么也将它们注释掉。

<httpProtocol>      
    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    ...
  </customHeaders>
</httpProtocol>-->

【讨论】:

  • 我在代码中有一个 EnableCors,在 web.config 文件中有一个条目,导致它被添加到响应头两次
猜你喜欢
  • 2018-09-19
  • 2019-06-19
  • 2019-02-07
  • 2016-05-13
  • 2021-08-12
  • 2018-03-28
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
相关资源
最近更新 更多