Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wpqs opened this issue Jul 30, 2020 · 2 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@wpqs
Copy link

wpqs commented Jul 30, 2020

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

  • added as shown above

Startup class

  • ConfigureServices(IServiceCollection services) - added services.AddHttpContextAccessor();

Index class

  • Ctor initialised _httpContextAccessor as shown above

  • Index.OnGet() - creates cookies , invokes its Set method as shown above, and uses return value to decide setting of Comment string - "fail", or display "fr-CH"

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

  • ASP.NET Core version 3.1
  • developed using VS2019 (16.5.5)
    .NET Core SDK (reflecting any global.json):
    Version: 3.1.202
    Commit: 6ea70c8dca

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]

@mkArtakMSFT mkArtakMSFT added the feature-platform Deprecated: Cross-cutting issues related to ASP.NET Core as a platform label Jul 30, 2020
@blowdart
Copy link
Contributor

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

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

@blowdart blowdart added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Jul 30, 2020
@ghost ghost added the Status: Resolved label Jul 30, 2020
@Tratcher Tratcher added area-servers and removed feature-platform Deprecated: Cross-cutting issues related to ASP.NET Core as a platform labels Jul 30, 2020
@wpqs
Copy link
Author

wpqs commented Jul 31, 2020

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

@wpqs wpqs closed this as completed Jul 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Sep 2, 2020
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

5 participants