2017-02-17 67 views
0

我已经写了下面的代码像下面刷新用户角色后,他们订阅我的网站像下面的具体时间到期:Asp.NET身份设置永久性Cookie后

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. 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? 
+0

您是否确定它是cookie,并且在让它们访问这些功能之前不检查身份验证? – BillRuhl

+0

@BillRuhl是100%,我设置了登录时的支票,检查PayPal上的订阅个人资料的状态是否有效,暂停或取消......但问题是,我发现一些用户从未从应用程序注销过很多),这个cookie留在他们的浏览器永不过期,他们取消了他们的订阅,如果他们从不重新登录,他们在我网站上的订阅基本上永远不会过期... – User987

+0

我不知道是否可以强制通过mvc应用程序迫使每个人的cookies访问到网站后过期...这将立即解决我的问题 – User987

回答

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

    } 

这就是我如何设置它,它适用于我。

+0

,谢谢,我似乎无法弄清楚什么是类ApplicationUserManager,ApplicationUser?自从我制作了一个空的mvc项目以来,我似乎没有在我的应用程序中使用这些内容? – User987

+0

使用系统; 使用Microsoft.AspNet.Identity; 使用Microsoft.AspNet.Identity.Owin;使用Microsoft.Owin的 ; 使用Microsoft.Owin.Security.Cookies; 使用Microsoft.Owin.Security.Google; 使用Owin; ......这些都是在我开始了类 – BillRuhl

+0

顶部我似乎有所有这些引用的的usings除了Microsoft.Owin.Security.Google,不知道这包是谁的? – User987