i use in my app authentication type called application cookie i want when the user close the browser the cookie must be expired auto
again the user try open the application
it is supposed the application ask the user about username and password
how can i solve that ?
by the way, i use .net framework standard mvc

Nikunj SatasiyaRank #302 · 8.3K reputationPosted Jul 24, 2024, 4:57 AM EDT
Mohamed,
In order to ensure that the authentication cookie in your ASP.NET MVC application expires when the user closes their browser, you need to configure the cookie authentication to use a session-based cookie rather than a persistent cookie. A session cookie is deleted when the browser session ends (i.e., when the browser is closed).
Here’s how you can configure this in an ASP.NET MVC application using the .NET Framework.
In your
Startup.Auth.csfile, which is typically found in theApp_Startfolder of your ASP.NET MVC project, you need to modify the settings in theCookieAuthenticationOptionswhen configuring theUseCookieAuthenticationmiddleware.Here's an example of how you can configure it
public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { // Enables the application to validate the security stamp when the user logs in. // This is a security feature which is used when you change a password or add an external login to your account. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) }, SlidingExpiration = true, ExpireTimeSpan = TimeSpan.FromMinutes(30), CookieHttpOnly = true, // Setting the CookieExpiration to null makes it a session cookie CookieExpiration = null }); }Important points:
nullto make the cookie a session cookie, which means it will expire automatically when the browser is closed.Mohamed abdelkader53 reputationPosted Jul 24, 2024, 8:08 AM EDT
thanks engineer Nikunj but i don't find CookieExpiration = null in new CookieAuthenticationOptions
it does not appear as property related to this class
Arthur Landry6 reputationPosted Dec 27, 2024, 9:52 PM EST
Thanks for your sharing. This is what I am looking for.