dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

After adding a cookie it does NOT become immediately available in the HttpRequest.Cookies collection #24442

Closed wpqs closed 4 years ago

wpqs commented 4 years ago

Describe the bug

The Microsoft Docs says in remarks "After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client". However, I find that a cookie can't be read immediately after setting, though it does appear in the browser's storage inspector and is read successfully after a page reload.

public class MyCookies
{
   public IHttpContextAccessor Accessor { get; private set; }

   public MyCookies(IHttpContextAccessor accessor)
   {
      Accessor = accessor;
   }

   public bool Set(string name, string value)
    {
       var rc = false;
       Accessor.HttpContext.Response.Cookies.Append(name, value);
       if (Accessor.HttpContext.Request.Cookies.TryGetValue(name, out var result) && (result == value))
          rc = true;
      return rc;
   }

   public string Get(string name)
   {
      var rc = "fail";
      if (Accessor.HttpContext.Request.Cookies.TryGetValue(name, out var result)) 
        rc = $"{name}={result}"; 
      return rc;
   }
}

public class IndexModel : PageModel
{
   private readonly IHttpContextAccessor _httpContextAccessor;

   [BindProperty]
   public string Comment { get; set; }
   public IndexModel(IHttpContextAccessor httpContextAccessor)
   {
         _httpContextAccessor = httpContextAccessor;
   }

   public void OnGet()
   {
        var result = true;
        var cookies = new MyCookies(_httpContextAccessor);
        if (cookies.Set(".AspNetCore.Culture", "fr-CH") == false)
           result = false;
        Comment = (result) ? $"Success: {cookies.Get(".AspNetCore.Culture")}" : $"{cookies.Get(".AspNetCore.Culture")}";
   }
}

To Reproduce

Repro Project BugCookieNotSet Created using VS2019 (16.5.5) using template ASP.NET Core Web Application C#, ASP.NET Core 3.1.

Changes:

MyCookies class

Startup class

Index class

Build and Start without Debugging

Index page displays 'fail' as cookies.Set() returned false. This shows the cookie is NOT immediately available in the HttpRequest.Cookies collection. However the expected value 'fr'CH' is displayed upon page reload.

Exceptions (if any)

Further technical details

Runtime Environment: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.202\

Host (useful for support): Version: 3.1.4 Commit: 0c2e69caa6

.NET Core SDKs installed: 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.402 [C:\Program Files\dotnet\sdk] 2.1.403 [C:\Program Files\dotnet\sdk] 2.1.500 [C:\Program Files\dotnet\sdk] 2.1.504 [C:\Program Files\dotnet\sdk] 2.1.509 [C:\Program Files\dotnet\sdk] 2.2.104 [C:\Program Files\dotnet\sdk] 3.0.101 [C:\Program Files\dotnet\sdk] 3.1.202 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

blowdart commented 4 years ago

You're looking at documentation for .NET Framework, not .NET Core.

The Core documentation has no such note, and the behaviour is expected,

wpqs commented 4 years ago

I feared that was the case. I was misdirected as selecting .NET Core 3.1 from the Framework docs for HttpRequest.Cookies Property produces a message "The requested page is not available for .NET Core 3.1. You have been redirected to the newest product version this page is available for." Clearly the page is available.

I have now raised a feature request in the hope that .NET Core will implement the same behaviour as Framework in respect of making cookies immediately available in the HttpRequest.Cookies collection - see issue 24529