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:
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!
