在WebAPI响应中创建的Cookie永远不会在随后的客户端请求中发送。寻找具有往返服务器 - >客户端>服务器的示例 [英] Cookie created in WebAPI response is never sent in subsequent client requests. Seeking example with round trip Server->Client->Server

查看:144
本文介绍了在WebAPI响应中创建的Cookie永远不会在随后的客户端请求中发送。寻找具有往返服务器 - >客户端>服务器的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.Net Core 2 WepAPI控制器。

当前版本的Chrome(当前为64)。

Angular 5 SPA。

需要在本地主机上工作。



这是控制器方法:

  public class TestController:ControllerBase 
{
[HttpGet,Route(api / [controller] / test)]
public async Task< IActionResult> Get()
{
Response.Cookies.Append(testcookie,testvalue,new CookieOptions
{
Path =/,
Domain = localhost,
Expires = DateTime.UtcNow.AddHours(6),
HttpOnly = false,
Secure = false
});

return Ok(Test Ok。);
}
}

我认为这不重要,但是这个是我的客户端代码。

  private async test2(){
const res = await this.http.get(' HTTP://本地主机:59879 / API /测试/试验').toPromise();
}

当我在Chrome控制台 - >网络标签 - >请求行 - >饼干标签;我看到我的响应cookie(s),但没有请求cookie。我也没有在 HttpContext.Request.Cookies 后面的请求中看到请求cookie。



如何创建任何类型的cookie




  • 创建服务器端,并从控制器方法返回

  • 和客户端/浏览器发送到服务器?



假设服务器位于localhost:59879。 b
$ b

我已经尝试了许多将 Domain 设置为localhost,127.0.0.1,false并将其排除的迭代。尝试完全没有指定 CookieOptions ,没有 Expires ,以及 HttpOnly 安全设置无效。



以上是我尝试过的一些资源。 https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-cookies#cookies-in-web-apirel =nofollow noreferrer> ASP.NET Web中的HTTP Cookie API

  • localhost上的Cookie与显式域

  • 未设置Chrome本地主机cookie

  • 使用Google搜索引擎进行搜索:net core 2 cookie -authentication


    更新 - 我看到最近的 SO 表示可能与我所遇到的情况相同。

    解决方案

    此代码:

      public class TestController:ControllerBase 
    {
    [HttpGet,Route(api / [controller] / test)]
    public async任务< IActionResult> Get()
    {
    Response.Cookies.Append(testcookie,testvalue,new CookieOptions
    {
    Path =/,
    Domain = localhost,
    Expires = DateTime.UtcNow.AddHours(6),
    HttpOnly = false,
    Secure = false
    });

    return Ok(Test Ok。);
    }
    }

    如果您通过浏览器访问,它将起作用。您尝试通过ajax API进行访问,它不会像那样工作。如果你在这里分析代码:

      Response.Cookies.Append(testcookie,testvalue,new CookieOptions 
    {
    Path =/,
    Domain =localhost,
    Expires = DateTime.UtcNow.AddHours(6),
    HttpOnly = false,
    Secure = false
    });

    这个响应不是通过浏览器访问的,它访问返回ajax成功。



    请参阅 can-an-ajax-response- set-a-cookie 线程。
    或者 mapping-header-cookie-string中的解决方案



    这不是更好的方式通过服务器访问API和set-cookies,更好的办法是在客户端上执行一边讨论这个线程


    I am using ASP.Net Core 2 WepAPI controller.
    Current version of Chrome(currently 64).
    Angular 5 SPA.
    Need to work on localhost.

    This is controller method:

    public class TestController : ControllerBase
    {
        [HttpGet, Route("api/[controller]/test")]
        public async Task<IActionResult> Get()
        {
            Response.Cookies.Append("testcookie", "testvalue", new CookieOptions
            {
                Path = "/",
                Domain = "localhost",
                Expires = DateTime.UtcNow.AddHours(6),
                HttpOnly = false,
                Secure = false
            });
    
            return Ok("Test Ok.");
        }
    }
    

    And I don't think it matters, but this is my client code.

      private async test2() {
        const res = await this.http.get('http://localhost:59879/api/test/test').toPromise();
      }
    

    When I look in Chrome Console -> Network Tab -> the request line -> Cookies Tab; I see my response cookie(s) but no request cookie. I also do not see request cookie in HttpContext.Request.Cookies on subsequent requests.

    How do I create any kind of cookie,

    • that is created server side, and returned from controller method,
    • and client/browser sends to server?

    Assume server is on localhost:59879.

    I have tried many iterations of setting the Domain to localhost, 127.0.0.1, false and excluding it. Tried not specifying CookieOptions at all, no Expires, and various combinations of HttpOnly and Secure settings to no avail.

    These are some resources I have tried.

    Update - I see a recent SO that may have the same cause of what I am experiencing.

    解决方案

    This code:

    public class TestController : ControllerBase
    {
        [HttpGet, Route("api/[controller]/test")]
        public async Task<IActionResult> Get()
        {
            Response.Cookies.Append("testcookie", "testvalue", new CookieOptions
            {
                Path = "/",
                Domain = "localhost",
                Expires = DateTime.UtcNow.AddHours(6),
                HttpOnly = false,
                Secure = false
            });
    
            return Ok("Test Ok.");
        }
    }
    

    it will work if you accessing by browser. You trying access via ajax api, it won't work like that. If you analyze the code here:

    Response.Cookies.Append("testcookie", "testvalue", new CookieOptions
    {
        Path = "/",
        Domain = "localhost",
        Expires = DateTime.UtcNow.AddHours(6),
        HttpOnly = false,
        Secure = false
    });
    

    this response is not access by browser, it access return ajax success.

    See here can-an-ajax-response-set-a-cookie thread. Or solution in mapping-header-cookie-string-to-cookiecollection-and-vice-versa.

    That's not better approach access API and set-cookies via server, better it just do it on client side as discussion this thread.

    这篇关于在WebAPI响应中创建的Cookie永远不会在随后的客户端请求中发送。寻找具有往返服务器 - &gt;客户端&gt;服务器的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆