locked
How to delete cookie after closing the tab while other tabs are open RRS feed

  • Question

  • User-259252065 posted

    I'm implementing an asp.net core 3.1 project. My problem is I want when the user close the project tab meanwhile other tabs are open, the cookie goes to get deleted. Right now after running the project, while other tabs are open, after I close the tab that shows my program and rerun my project again, I don't need to login and the cookie isn't deleted. But I want in that situation to login as well. I appreciate if anyone tells me the expression I need to use to solve the issue. For implementing the project, I authenticate the user via ldap with the below expressions in Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
    
    services.AddControllersWithViews();
    
    services.AddDbContext<CSDContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("CSDContext")));
    
    
    services.AddScoped<IAuthenticationService, LdapAuthenticationService>();
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
    {
    // Cookie settings
    
    //set the cookie name here
    options.Cookie.Name = "UserLoginCookie"; // Name of cookie
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(15);
    
    options.LoginPath = "/Account/Login";
    options.AccessDeniedPath = "/Account/UserAccessDenied";
    options.AccessDeniedPath = "/Account/AccessDenied";
    options.SlidingExpiration = true;
    });
    
    services.AddSession();
    services.AddSingleton<MySharedDataViewComponent>();
    services.AddHttpContextAccessor();
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
    }
    app.UseSession();
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    
    });
    }
    
    

    Now I used the below expression for deleting cookie after closing the browser.

    var authProperties = new AuthenticationProperties
    {

    IsPersistent = false

    }; 

    Wednesday, August 19, 2020 4:38 AM

All replies

  • User475983607 posted

    Elenorarez

    My problem is I want when the user close the project tab meanwhile other tabs are open, the cookie goes to get deleted. Right now after running the project, while other tabs are open, after I close the tab that shows my program and rerun my project again, I don't need to login and the cookie isn't deleted. But I want in that situation to login as well.

    Cookies are tied to the browser instance not a tab.   JavaScript cannot see HTTP cookies.  Closing a browser does not send an HTTP request to delete a cookie. 

    You'll need to design the application to work with browsers.

    Wednesday, August 19, 2020 11:07 AM