【问题标题】:ASPXAUTH cookie not being stored after CORS request在 CORS 请求后未存储 ASPXAUTH cookie
【发布时间】:2020-05-17 11:59:50
【问题描述】:

有人要求我编写一个 javascript/HTML 前端来连接到一组 WCF 服务。我可以使用 Postman 访问登录服务,我可以看到在有效登录时,设置了两个 cookie .ASPXAUTHASP.NET_SessionId

当我从我的 javascript 代码中访问相同的服务时,我会收到一个 200 响应,并且在 Chrome 开发人员工具的网络部分中,我可以在两个 cookie 中的每一个的响应中看到 Set-Cookie 标头。

但是,cookie 不会存储在浏览器中,因此对服务器的后续请求会失败,因为它们缺少 cookie 凭据。

客户端应用程序位于与服务器 (http://localhost:3101) 不同的域 (https://localhost:44357) 上,因此 CORS 正在发挥作用。客户端调用是使用aurelia-http-client 进行的,它是XMLHttpRequest 的包装器。我正在使用.withCredentials(),它应该添加credentials: true 标头。您可以看到它被包含在内:

服务器为 CORS 配置如下:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","https://localhost:44357");
    if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Cache-Control","no-cache");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers","Content-Type,Accept,credentials");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials","true");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age","1728000");
    }
}

我错过了什么?为什么浏览器不存储来自 WCF 服务器的 cookie?

【问题讨论】:

    标签: javascript wcf cookies


    【解决方案1】:

    在玩的过程中,我发现将 Access-Control-Allow-Credentials 标头移到 if 语句之外就可以了。

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",...)
        //Had to move this line outside of the if statement
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials","true");
        if(...)
        {
           ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-26
      • 2021-05-29
      • 2013-05-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2016-07-21
      • 2012-04-26
      • 2021-11-19
      相关资源
      最近更新 更多