2015-11-20 60 views
3

我試圖在Owin訪問過期時間我用下面的例子訪問Cookie有效期owin

Access ExpireTimeSpan property of Owin Cookie Authentication to notify user of login expiry

,但我不能去上班。我得到一個錯誤的關鍵,價值確實存在。我真的很感激,如果有人能指出我正確的方向,謝謝。

StartupAuth.cs

 var config1 = new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      //LoginPath = new PathString("/Account/Login"), 
      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, ApplicationUser>(
        validateInterval: TimeSpan.FromMinutes(30), 
        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 

      } 
     }; 

     app.UseCookieAuthentication(config1); 
     var options = new CookieAuthenticationOptions 
     { 
      // usual options such as LoginPath, for example, go here... 
      LoginPath = new PathString("/Account/Login"), 
      Provider = new CookieAuthenticationProvider 
      { 
       OnValidateIdentity = context => 
       { 
        DateTimeOffset now = DateTimeOffset.UtcNow; 

        context.OwinContext.Request.Set<double>("time.Remaining", 
          context.Properties.ExpiresUtc.Value.Subtract(now).TotalSeconds); 

        return Task.FromResult<object>(null); 
       } 
      } 
     }; 
     app.UseCookieAuthentication(options); 

控制器

[Authorize] 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     //var secondsRemaining = (double)Request.GetOwinContext() 
     //       .Environment["time.Remaining"]; 
     return View(); 
    } 

} 

回答