.net – 如何知道OWIN cookie何时过期?

前端之家收集整理的这篇文章主要介绍了.net – 如何知道OWIN cookie何时过期?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想根据OWIN cookie到期的时间创建一些倒数计时器.我正在使用OWIN与MVC 5,从我的理解SlidingExpiration默认情况下打开.我不使用’会话’,因为我需要这个应用程序生活在一个网络农场(我不打算部署一个会话数据库).

解决方法

所有您需要的是在Cookie验证阶段获取CookieValidateIdentityContext.一旦你得到它,提取任何你需要的,并保持他们作为声明或其他方式,你喜欢.

对于具有Asp.NET Identity 2.0的MVC 5,您需要执行两个步骤:

>定义自定义OnValidateIdentity,提取cookie信息,并将其保留为声明.

  1. public class Startup
  2. {
  3. public void Configuration(IAppBuilder app)
  4. {
  5. app.UseCookieAuthentication(new CookieAuthenticationOptions
  6. {
  7. AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,Provider = new CookieAuthenticationProvider
  8. {
  9. OnValidateIdentity = MyCustomValidateIdentity //refer to the implementation below
  10. }
  11. }
  12. }
  13.  
  14.  
  15. // this method will be called on every request
  16. // it is also one of the few places where you can access unencrypted cookie content as CookieValidateIdentityContext
  17. // once you get cookie information you need,keep it as one of the Claims
  18. // please ignore the MyUserManager and MyUser classes,they are only for sample,you should have yours
  19. private static Task MyCustomValidateIdentity(CookieValidateIdentityContext context)
  20. {
  21. // validate security stamp for 'sign out everywhere'
  22. // here I want to verify the security stamp in every 100 seconds.
  23. // but I choose not to regenerate the identity cookie,so I passed in NULL
  24. var stampValidator = SecurityStampValidator.OnValidateIdentity<MyUserManager<Myuser>. MyUser>(TimeSpan.FromSeconds(100),null);
  25. stampValidator.Invoke(context);
  26.  
  27. // here we get the cookie expiry time
  28. var expireUtc = context.Properties.ExpiresUtc;
  29.  
  30. // add the expiry time back to cookie as one of the claims,called 'myExpireUtc'
  31. // to ensure that the claim has latest value,we must keep only one claim
  32. // otherwise we will be having multiple claims with same type but different values
  33. var claimType = "myExpireUtc";
  34. var identity = context.Identity;
  35. if(identity.HasClaim(c=> c.Type == claimType))
  36. {
  37. var existingClaim = identity.FindFirst(claimType);
  38. identity.RemoveClaim(existingClaim);
  39. }
  40. var newClaim = new Claim(claimType,expireUtc.Value.UtcTicks.ToString());
  41. context.Identity.AddClaim(newClaim);
  42.  
  43. return Task.FromResult(0);
  44. }
  45. }

>在控制器方法中访问您的声明

  1. // since expiry time has now become part of your claims,you now can get it back easily
  2. // this example just returns the remaining time in total seconds,as a string value
  3. // assuming this method is part of your controller methods
  4.  
  5. public string RemainingTime()
  6. {
  7. var identity = User.Identity as ClaimsIdentity;
  8. var claimType = "myExpireUtc"; //NOTE: must be the same key value "myExpireUtc" defined in code shown above
  9.  
  10. if(identity != null && identity.HasClaim(c=> c.Type == claimType))
  11. {
  12. var expireOn = identity.FindFirstValue(claimType);
  13.  
  14. DateTimeOffset currentUtc = DateTimeOffset.UtcNow;
  15. DateTimeOffset? expireUtc = new DateTimeOffset(long.Parse(expireOn),TimeSpan.Zero);
  16.  
  17. var remaining = (expireUtc.Value - currentUtc).TotalSeconds;
  18.  
  19. return remaining.ToString();
  20. }
  21. return string.Empty;
  22. }

我使用这种方法提醒我的应用程序用户在会话超时之前扩展会话.

感谢这篇文章How do I access Microsoft.Owin.Security.xyz OnAuthenticated context AddClaims values?

猜你在找的HTML相关文章