zoukankan      html  css  js  c++  java
  • How to set asp.net Identity cookies expires time

    If IsPersistent property of AuthenticationProperties is set to false, then the cookie expiration time is set to Session.

    If checkbox "remember me" is checked then AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true }, userIdentity); will create a cookie with expiration time equal to ExpireTimeSpan you set up in Startup.cs (defaults to 14days).

    If checkbox "remember me" is NOT checked then you have to use AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30)}, userIdentity);. Again IsPersistent is set to true but now we give a value to ExpiresUtc so it does not use from CookieAuthenticationOptions from Startup.cs.

    public override async Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser)
    {
        var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture();
        // Clear any partial cookies from external or two factor partial sign ins
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
        if (rememberBrowser)
        {
            var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id));
            AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity);
        }
        else
        {
            //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity);
            if (isPersistent)
            {
                AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity);
            }
            else
            {
                AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30) }, userIdentity);
            }        
        }
    }
    
  • 相关阅读:
    S5PV210 NAND Flash
    S5PV210串口
    S5PV210初始化系统时钟
    每日英语:Dishing the Dirt on Hand-Washing Guidelines
    每日英语:Vender Assault Shines Ugly Light on China's Urban Enforcers
    每日英语:The Perils Of Giving Advice
    每日英语:China Targets Big Pharma
    每日英语:Asia Has World's Biggest Pay Gap, Study Finds
    每日英语:Now on Taobao: Outsourced Care for Grandma
    每日英语:The Upside of Favoritism
  • 原文地址:https://www.cnblogs.com/dupeng0811/p/how-to-set-asp-net-identity-cookies-expires-time.html
Copyright © 2011-2022 走看看