使用 Reactjs + Redux + Saga 设置 ASP.NET Core。当 asp.net 核心会话过期时,它需要通知用户。但问题是通过发送 GET 请求来检查我们扩展会话的会话状态,这意味着会话将永远不会结束,除非浏览器中的选项卡将被关闭(然后 GET 请求将不会被发送)。这是会话设置Startup.cs
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(60 * 60);
options.Cookie.HttpOnly = true;
});
然后我们每 5 分钟从客户端发送一次请求以获取身份:
function* checkSessionIsValid() {
try {
const response = yield call(axios.get, 'api/Customer/FetchIdentity');
if (!response.data) {
return yield put({
type: types.SESSION_EXPIRED,
});
yield delay(300000);
return yield put({ type: types.CHECK_SESSION_IS_VALID });
}
return;
} catch (error) {
return;
}
}
后端端点(_context is IHttpContextAccessor
):
[HttpGet("FetchIdentity")]
public LoginInfo GetIdentity()
{
if (SessionExtension.GetString(_context.Session, "_emailLogin") != null)
{
return new LoginInfo()
{
LoggedInWith = "email",
LoggedIn = true,
Identity = ""
};
}
return null;
}
所以我们从SessionExtension
. 但是可能有某种方法可以在不连接到后端的情况下获得它?