2017-02-17 115 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