in Education by
I created a sample web application with ASP.NET Core to test the storage and retrieval of the cookie. Unfortunately, I'm not able to store cookies. I've read these questions for my problem: Cookies in ASP.NET Core rc2 Create cookie with ASP.NET Core Cookies and ASP.NET Core But unfortunately I did not find a good answer. I will first request an IndexA action to store the cookie. The code runs without any errors,but when I request IndexB action. Does not find the specified value This is IndexA Action: public IActionResult IndexA() { Response.Cookies.Append("mykey", "myvalue", new CookieOptions() { Expires = DateTime.Now.AddMinutes(10) }); return View(); } This is IndexB Action: public IActionResult IndexB() { var cookie = Request.Cookies["mykey"]; return View(); } And This is Startup Class: public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I think it might have to do with the GDPR-related feature that ASP.NET Core 2.1 ships with. My understanding is that it allows websites to define which cookies are essential or not for the browser, and the non-essential ones won't be sent. The first thing I can think of is to simply remove the check for consent for non-essential cookies. Do not do this in production as you might violate regulation!. Change: services.Configure(options => { options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); to services.Configure(options => { // No consent check needed here options.CheckConsentNeeded = context => false; options.MinimumSameSitePolicy = SameSiteMode.None; }); The second potential way of fixing this is declaring your cookie as essential, so no matter what the consent check result is, your cookie will be sent over to the browser. To do this, change: Response.Cookies.Append( "mykey", "myvalue", new CookieOptions() { Expires = DateTime.Now.AddMinutes(10) }); with Response.Cookies.Append( "mykey", "myvalue", new CookieOptions() { Expires = DateTime.Now.AddMinutes(10), // Marking the cookie as essential IsEssential = true });

Related questions

0 votes
    I'm new to React with Redux and I've been working on a new web application that has some basic ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have an ASP.Net core 1.1 website and I want to embed Power BI reports into the site. Azure ... JsonConvert.DeserializeObject(result); Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am developing a .NET core application with Identity login which provides a Bootstrap form In the /Identity ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I'm doing a simple dotnet restoreon my mac. I'm using the (for now) latest dotnet CLI version 2. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    What are Cookies in ASP.NET?...
asked Apr 6, 2021 in Education by JackTerrance
0 votes
    For example, I have an ASP.NET form that is called by another aspx: string url = "http:// ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    Can ASP.NET Core work with the .NET framework?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    I have the following WEB API method, and have a SPA template with Angular: [HttpPost] public IActionResult ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I've got a method that reads settings from my config file like this: var value = ConfigurationManager. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have an AWS Lambda function built in .NET Core 2.1. Which is triggered by a SQS Queue. This ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I create a project with "dotnet new angular". When a breakpoint is set on a Typescript instruction, it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    i am using serilog in asp net core application and using a json formatter to create a daily log file ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have an AWS Lambda function built in .NET Core 2.1. Which is triggered by a SQS Queue. This ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Is there any way to add a custom parameter to identityserver auth request. I tried this way. but it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    F# allows constraining generic types on the type's members, similar to: type ClassWithMemberConstraint = class end ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
...