gpt4 book ai didi

c# - 访问cookie过期时间owin

转载 作者:行者123 更新时间:2023-11-30 17:40:49 25 4
gpt4 key购买 nike

我正在尝试访问 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);

Controller

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

}

最佳答案

这个答案有另一种存储值的方法。它将其存储为声明。你可以试试这个:How to know when OWIN cookie will expire?

关于c# - 访问cookie过期时间owin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com