访问cookie过期时间 [英] Accessing cookie expiration time owin
本文介绍了访问cookie过期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下示例访问Owin的到期时间
I am trying to access expiration time on Owin i am using the following example
Owin的Access ExpireTimeSpan属性Cookie身份验证,用于通知用户登录到期
但是我不能上班.我收到一个错误的钥匙,价值确实存在.如果有人能指出正确的方向,我将非常感谢.
but i cant get to work. I get an error the key, value does exist. I would really appreciate if someone could point me to the right direction thanks.
StartupAuth.cs
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();
}
}
推荐答案
此答案提供了另一种存储值的方法.它将其存储为索赔.您可以尝试以下方法:如何知道OWIN Cookie何时到期?
This answer has an alternative approach of storing the value. It stores it as a claim. You could try this instead: How to know when OWIN cookie will expire?
这篇关于访问cookie过期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文