Search code examples
asp.netasp.net-mvccookiesasp.net-identitysetcookie

Asp.NET Identity setting persistent cookie to expire after specific time


I have written a following code like below to refresh user roles after they subscribed to my website like following:

private void RefreshUserRoles()
{
    var AuthenticationManager = HttpContext.GetOwinContext().Authentication;
    var Identity = new ClaimsIdentity(User.Identity);

    Identity.RemoveClaim(Identity.FindFirst(ClaimTypes.Role));
    Identity.AddClaim(new Claim(ClaimTypes.Role, "Subscriber"));

    AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
        new ClaimsPrincipal(Identity), 
        new AuthenticationProperties { IsPersistent = true}
    );
}

Please note this line of code:

AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
    new ClaimsPrincipal(Identity), 
    new AuthenticationProperties { IsPersistent = true}
    );

After user comes back to my website I set the cookie to be persistent, but I forgot to set the expiration for this cookie. I should set this cookie to last for 30 minutes, after which user should be asked to re-log onto the system.

Since some users never re-log on website, this leaves me with an issue, now I need to reset all users cookies in their browsers when they access the website and change their role if they cancelled the subscription. I noticed some users cancelled their subscription and never relogged but yet they still are able to use features on my website...

So my questions are:

  1. How to set expiration of cookie to 30 minutes after which user will be asked to re-log onto the system.

  2. How to Setup to re-check users cookies if they haven't expired in a long time so that I can reset their cookies to regular user if they cancelled subscription?


Solution

  • Here is the ConfigureAuth method I was talking about:

    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Controller/Action"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
    
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
    }
    

    That's how I have it set and it works for me.