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)