It is possible. One way to do this is to use a callback OnValidateIdentitythat is called every time a cookie is authenticated, that every time a request is made in a web application (subject to active mode).
var options = new CookieAuthenticationOptions
{
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);
Here I save the seconds remaining in the OWIN environment dictionary. You can use it from any place where the dictionary is available, and inform the user. For example, from an MVC, you can do something like this.
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
var secondsRemaining = (double)Request.GetOwinContext()
.Environment["time.Remaining"]);
return View();
}
}
Badri source
share