更改默认ASP.NET身份双因素记住Cookie过期时间

时间:2015-06-22 12:36:50

标签: c# asp.net-mvc asp.net-identity-2 remember-me two-factor-authentication

我一直在使用ASP.NET Identity 2.2.1。以下是VerifyCode操作的post方法中的代码。

var result = await SignInManager.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent: model.RememberMe, rememberBrowser: model.RememberBrowser);

switch (result)
{
    case SignInStatus.Success:
        return RedirectToAction("Dashboard","Index");
    case SignInStatus.LockedOut:
        return View("Lockout");
    case SignInStatus.Failure:
    default:
        ModelState.AddModelError("", "Invalid code.");
        return View(model);
}

当model.RememberMe和model.RememberBrowser都为true时,浏览器会记住Identity和两个因子cookie 2周。这是默认实现。

但我只需要记住TFA 8个小时。我怎样才能做到这一点?

自从过去10天以来,我一直在寻找解决方案,但我还没有找到解决方案。任何帮助将不胜感激。

以下是我的StartUp类中的代码。它只是没有生效。

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

        string domainName = string.IsNullOrEmpty(Config.DomainName) ? "" : Config.DomainName;
        string cookieName = "AspNet." + domainName;

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            SlidingExpiration = true,
            ExpireTimeSpan = TimeSpan.FromHours(9),
            CookieDomain = domainName,
            CookieName = cookieName,
            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<ApplicationUserManager, ProgenyUser, long>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                    getUserIdCallback: (id) => (id.GetUserId<long>()))
            }
        });



        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        //app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        // Enables the application to remember the second login verification factor such as phone or email.
        // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        // This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);


        // Uncomment the following lines to enable logging in with third party login providers
        //app.UseMicrosoftAccountAuthentication(
        //    clientId: "",
        //    clientSecret: "");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        //app.UseGoogleAuthentication();
    }
}

2 个答案:

答案 0 :(得分:1)

这似乎是一个查看以下GitHub问题和处理此问题的SO上的其他线程的错误。 https://github.com/aspnet/Identity/issues/309

看起来它已修复当前的Identity测试版,但这可能不适合您。我会在下面讨论建议的工作 SO question

答案 1 :(得分:0)

我参加聚会有点晚了,但遇到了与OP相同的问题,但就我而言,我需要将2FA cookie推迟2个多星期。我正在使用2.2.1,实际上发现Magnus帖子中github帖子中的代码对我有用。为了便于参考,我将在此处包括实际代码。请注意,我从Startup.Auth代码中注释掉了原始app.UseTwoFactorRememberBrowserCookie。我希望这可以节省别人给我带来的讨厌的头痛。

"query": {
    "bool": {
        "must_not": {
            "nested": {
                "path": "text_terms",
                "query": {
                    "term": {
                        "text_terms.term": "西门子"
                    }
                }
            }
        }
    }
}