1

I have created a simple web cookie from my ASP.NET web form application. I am trying to retrieve this cookie from a separate.NET Core web application. Whenever I try to do this, the .NET Core application keeps returning null values for the cookie.

This is how the cookie is created in the ASP.NET web form application:

 protected void btn1_Click(object sender, EventArgs e)
        {
            HttpCookie Abc = new HttpCookie("Abc");
            DateTime now = DateTime.Now;

            //Abc Set the cookie value.
            Abc.Value = txt1.Text;
            // Set the cookie expiration date.
            Abc.Expires = now.AddMinutes(1);

            // Add the cookie.
            Response.Cookies.Add(Abc);

       }

This is how I am trying to read this "Abc" cookie from the .NET Core application:

public void OnGet()
        {

         if (HttpContext.Request.Cookies["Abc"] != null)
            {
                Message = "ya";
            }
            else
            {
                Message = "no";
            }
        }

Here is the Startup.cs details for the ASP.NET CORE app:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddDistributedMemoryCache();
          //  services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddHttpContextAccessor();
            services.AddSession(options =>
            {
                options.Cookie.HttpOnly = true;
                // Make the session cookie essential
                options.Cookie.IsEssential = true;
            });

            services.Configure<CookiePolicyOptions>(options =>
            {
                // No consent check needed here
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSession();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }

I am able to find the cookie in the browser as expected when I run the ASP .NET Core app:

enter image description here

I have spent quite a few hours researching this but was not successful. Any ideas as to why I cannot read the cookie from the .Net Core app? I greatly appreciate any feedback.

Thank you!

2
  • What version of .Net Core are you using? Commented Jan 28, 2020 at 22:16
  • Hi @EdneyHolder, i am using netcoreapp3.0 Commented Jan 28, 2020 at 23:24

1 Answer 1

2

If your web apps are hosted in sub-domains of the same domain (e.g. app1.example.com and app2.example.com), then you can easily read cookies between sub-domains by setting the "Domain" property of the HttpCookie object to .example.com

HttpCookie Abc = new HttpCookie("Abc");
DateTime now = DateTime.Now;
Abc.Domain = ".example.com";
//Abc Set the cookie value.
Abc.Value = txt1.Text;
// Set the cookie expiration date.
Abc.Expires = now.AddMinutes(1);

// Add the cookie.
Response.Cookies.Add(Abc);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Anuraj, I am running both applications through visual studio - so they are both running on localhost.
It should be identical - if you use different port - it is a different domain
Ahhh that could be it. Thanks Anuraj!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.