Chr*_*ton 5 authentication cookies owin asp.net-mvc-5.2
我使用Owin的cookie身份验证在一段时间不活动后注销用户.问题是,我需要让用户知道他们的会话在"X"分钟内到期.
如何访问身份验证cookie以获取剩余时间?这甚至可能吗?以前有人不得不这样做吗?
有可能的.一种方法是使用OnValidateIdentity回调,每次对cookie进行身份验证时调用该回调,这是每次向Web应用程序发出请求时(假设活动模式).
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);
Run Code Online (Sandbox Code Playgroud)
在这里,我将存储OWIN环境字典中剩余的秒数.您可以从可访问字典的任何位置使用它并通知用户.例如,从MVC控制器,您可以执行类似的操作.
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
var secondsRemaining = (double)Request.GetOwinContext()
.Environment["time.Remaining"]);
// Do what you want to do with the secondsRemaining here...
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2223 次 |
| 最近记录: |