首页 文章

Http OPTION REQUEST与web api中的cors进行身份验证

提问于
浏览
1

我正在使用Microsoft.AspNet.WebApi.Cors来支持交叉源请求

https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

我的web apis配置为使用Windows身份验证,来自angular的每个请求都将withCredentials设置为true . 一切都在使用HTTP GET,但是PUT请求会发送预检请求,这会导致未经授权 . 我的问题是Microsoft.AspNet.WebApi.Cors是否支持OPTION请求的配置 .

1 回答

  • 0

    把它放在你的Global.asax.cs中

    (我确定你要么找到解决方案要么放弃了,但这是我在寻找解决方案时在Google上找到的链接 . )

    protected void Application_BeginRequest()
    {
        if (Request.HttpMethod == "OPTIONS")
        {
            Response.StatusCode = (int)HttpStatusCode.OK;
            Response.AppendHeader("Access-Control-Allow-Origin", Request.Headers.GetValues("Origin")[0]);
            Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            Response.AppendHeader("Access-Control-Allow-Credentials", "true");
            Response.End();
        }
    }
    

相关问题