Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to delete cookie after the browser closed in asp.net core

$
0
0

I'm implementing an asp.net core 3.1 project. My problem is I want when the user close the browser, the cookie goes to get deleted . For implementing the project, I authenticate the user via ldap with the below expression 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?}");

});
}

I have also a file  that is called, deleteCookie.js and its content is like the following:

function deleteCookie(name) {
setCookie(name, "", -1);
}
function setCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
$(window).unload(function () {
deleteCookie('UserLoginCookie');
});

function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

And in my index view, I've written following code to use the deleteCookie.js and use the deleteCookie() function.

<script language="JavaScript" type="text/javascript" src="~/js/deleteCookie.js"></script>

@section Scripts{
<script>
$(window).unload(deleteCookie('UserLoginCookie'));
console.log("coockie:" + getCookie('UserLoginCookie'));
</script>

}

But my code doesn't work. I appreciae if anyone helps me to solve the issue.


Viewing all articles
Browse latest Browse all 9386

Trending Articles