【问题标题】:Asp.NET Identity setting persistent cookie to expire after specific timeAsp.NET Identity 设置持久性 cookie 在特定时间后过期
【发布时间】:2017-07-07 02:35:13
【问题描述】:

我编写了如下代码,用于在用户订阅我的网站后刷新用户角色,如下所示:

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}
    );
}

请注意这行代码:

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

用户返回我的网站后,我将 cookie 设置为持久性,但我忘记设置此 cookie 的过期时间。我应该将此 cookie 设置为持续 30 分钟,之后应要求用户重新登录系统。

由于一些用户从不重新登录网站,这给我留下了一个问题,现在我需要在他们访问网站时重置他们浏览器中的所有用户 cookie,并在他们取消订阅时更改他们的角色。我注意到一些用户取消了他们的订阅并且从未重新登录,但他们仍然能够使用我网站上的功能...

所以我的问题是:

  1. 如何将 cookie 的过期时间设置为 30 分钟,之后将要求用户重新登录系统。

  2. 如果用户的 cookie 长时间未过期,如何设置以重新检查,以便在他们取消订阅时将其 cookie 重置为普通用户?

【问题讨论】:

  • 您确定这是 cookie,并且在让他们访问这些功能之前没有检查身份验证吗?
  • @BillRuhl 是的 100% ,我在登录时设置了检查,在贝宝上检查订阅配置文件的状态是活跃的、暂停的还是取消的......但问题是,我发现有些用户从来没有从应用程序注销(有很多),这个cookie留在他们的浏览器上永不过期,他们取消了他们的订阅,如果他们从不重新登录,他们在我网站上的订阅基本上永远不会过期......
  • 我不知道是否有可能通过 mvc 应用程序强制每个人的 cookie 在他们访问网站后过期......这将立即解决我的问题
  • 可能设置 cookie 过期在浏览器会话结束时过期...但我不知道该怎么做
  • 在我的一些 MVC 应用程序中,您可以在启动类中设置验证间隔,也就是超时。应该有一个您传递 CookieAuthenticationOptions 的 configureAuth 方法。其中一个选项是“Provider”,您分配一个新的“CookieAuthenticationProvider”,它具有“validateInterval”属性并将其设置为等于 TimeSpan

标签: asp.net asp.net-mvc cookies asp.net-identity setcookie


【解决方案1】:

这里是我说的 ConfigureAuth 方法:

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);
}

这就是我设置它的方式,它对我有用。

【讨论】:

  • 非常感谢,我似乎无法弄清楚 ApplicationUserManager、ApplicationUser 类是什么?自从我做了一个空的 mvc 项目后,我的应用程序中似乎没有这些?
  • 使用系统;使用 Microsoft.AspNet.Identity;使用 Microsoft.AspNet.Identity.Owin;使用微软.Owin;使用 Microsoft.Owin.Security.Cookies;使用 Microsoft.Owin.Security.Google; using Owin;.....这些是我的启动类顶部的使用
  • 我似乎有所有这些引用,除了 Microsoft.Owin.Security.Google,不确定这是哪个包?
  • 除非您让用户使用他们的 Google+ 帐户进行身份验证,否则您不需要它
  • 好的,我安装了这个,请问您的项目引用了哪些类:ApplicationUserManager、ApplicationUser、ApplicationSignInManager、ApplicationDbContext 我的项目中似乎缺少这些?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 2019-11-13
  • 1970-01-01
  • 2018-11-08
相关资源
最近更新 更多