[英]Accessing cookie expiration time owin
I am trying to access expiration time on Owin i am using the following example 我正在尝试使用以下示例访问Owin的到期时间
Access ExpireTimeSpan property of Owin Cookie Authentication to notify user of login expiry 访问Owin Cookie身份验证的ExpireTimeSpan属性,以通知用户登录到期
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);
Controller 控制者
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
//var secondsRemaining = (double)Request.GetOwinContext()
// .Environment["time.Remaining"];
return View();
}
}
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? 您可以尝试以下方法: 如何知道OWIN Cookie何时到期?
This is working but kind of ugly.. it accounts for the SlidingExpiration
logic .. note that the claim is empty on the signin request in my case so i just set it to the sessionTimeInMinutes
这是SlidingExpiration
但是很丑..它说明了SlidingExpiration
逻辑..请注意,在我的情况下,登录请求中的声明为空,因此我将其设置为sessionTimeInMinutes
SlidingExpiration = true,
CookieName = applicationCookieName,
Provider = new CookieAuthenticationProvider
{
// Not called on signin
OnValidateIdentity = context =>
{
// this is the last value before ExpireTimeSpan will update and its not reflected here
var expiresUtc = context.Properties.ExpiresUtc;
if ( expiresUtc == null) return Task.FromResult(0);
var sessionExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(sessionTimeInMinutes);
var expiresDifferenceSeconds =
sessionExpiresUtc.ToUnixTimeSeconds() - expiresUtc?.ToUnixTimeSeconds();
var expires = sessionTimeInMinutes * 60 / expiresDifferenceSeconds < 2
? sessionExpiresUtc
: expiresUtc;
context.Identity.UpdateClaim(CustomClaimTypes.Expires, expires.ToString());
return Task.FromResult(0);
}
}
then i have a filter that adds a expiration cookie that the app uses 然后我有一个过滤器,添加了应用程序使用的过期Cookie
DateTimeOffset expires;
if (identity.HasClaim(c => c.Type == CustomClaimTypes.Expires))
{
expires = DateTimeOffset.Parse(identity.FindFirstValue(CustomClaimTypes.Expires));
}
else
{
expires = DateTimeOffset.UtcNow.AddMinutes(sessionTimeInMinutes);
}
var cookieHeaderValues = new List<CookieHeaderValue>
{
new ApplicationCookeHeaderValue("session.expiresAt", expires.ToString("u"), expires),
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.