我正在构建一个C# (.net core 2.1) Razor Page网站。登录机制将基于此处的文档进行操作——因此在cookie中使用令牌(如果我理解正确):https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-2.1
假设我设置了3分钟的会话,并且如果用户正在积极使用网站,我将重置此值。如果他没有这样做,在令牌过期前30秒,我想向他显示消息,例如“要么做点事情,要么您将被登出”。
我的问题是,我找不到获取此“过期”时间的方法。以下是我验证和存储数据的方式:
现在,我该如何调用AuthenticationProperties来在所有页面上获取/设置ExpiresUtc上的数据呢?假设设置正在工作,因为我定义了参数AllowRefresh = true。但是我不知道如何获取。
在理想情况下,我希望像获取身份验证数据一样,在部分页面上获取它:
我的问题是,我找不到获取此“过期”时间的方法。以下是我验证和存储数据的方式:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
ReturnUrl = returnUrl;
int iCookieLifeTime = 10;
if (ModelState.IsValid)
{
var user = await AuthenticateUser(Input.UserName, Input.License, Input.Password, Input.KeepLogged);
if (user == null)
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.UserName),
new Claim("Password", user.Password ) )
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
AllowRefresh = true,
ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(iCookieLifeTime)
};
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
return LocalRedirect(Url.GetLocalUrl(returnUrl));
}
现在,我该如何调用AuthenticationProperties来在所有页面上获取/设置ExpiresUtc上的数据呢?假设设置正在工作,因为我定义了参数AllowRefresh = true。但是我不知道如何获取。
在理想情况下,我希望像获取身份验证数据一样,在部分页面上获取它:
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor;
@if (HttpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
(...)