locked
How to use Asp.Net Identity to achieve this effect RRS feed

  • Question

  • User-1580372550 posted

    I would like to use Identity to achieve this effect: 30 minutes without the operation, authorization expired.

    Thanks.

    Friday, May 6, 2016 3:37 PM

Answers

  • User614698185 posted

    Hi yuweiyuan,

    Identity already embeds expiry time in the cookie data and it is checked by OWIN.

    To limit cookie life set ExpireTimeSpan to 30 min in ConfigureAuth method in Startup.Auth.cs:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 9, 2016 8:37 AM

All replies

  • User-491950272 posted

    I've never done such a demo practically, but I've some suggestions here are:

    Use jQuery to listen to the Mouse Move (mousemove) event and make a form post open setting time as

    var timeout = null;
    
    $(document).on('mousemove', function() {
        clearTimeout(timeout);
    
        timeout = setTimeout(function() {
            $('#form').submit();
        }, 3000); // for example for 3 seconds of idle pointer.
    });

    And in the post action of the form, just use 

    await _signInManager.SignOutAsync();

    To get the user log out.

    Saturday, May 7, 2016 8:22 PM
  • User614698185 posted

    Hi yuweiyuan,

    Identity already embeds expiry time in the cookie data and it is checked by OWIN.

    To limit cookie life set ExpireTimeSpan to 30 min in ConfigureAuth method in Startup.Auth.cs:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 9, 2016 8:37 AM