gpt4 book ai didi

asp.net-core - 在 ASP.NET Core 中 session 过期之前显示 session 超时警告消息

转载 作者:行者123 更新时间:2023-12-05 08:17:22 31 4
gpt4 key购买 nike

我可以通过以下代码设置 session 结束。

services.AddSession(选项=> {
options.IdleTimeout = TimeSpan.FromMinutes(2);
});

我需要在 20 分钟后延长 session ,如果向用户显示 session 超时警告消息,以便用户可以从应用程序 UI 中延长他们的时间。

最佳答案

您的问题中已有 session 超时代码。顺便说一下,默认值为 20 分钟。如果您想了解更多信息,可以在 Configuring Session 阅读更多信息。 .

据我所知,ASP.NET 没有显示 session 过期通知消息的内置机制。所以,我们必须自己写。

Here是我的,here是用法。您可以随意使用它。 因为我使用 Kendo UI,所以我使用 Kendo UI 窗口作为对话框。如果您不想使用 Kendo UI,可以将其替换为 jQuery UI。

enter image description here

_SessionExpireNotification.cshtml

我保留里面的设置appsettings.json .您可以在此文件中对它们进行硬编码。

@using Asp.Core
@using Microsoft.Extensions.Options
@using Asp.Web.Common

@inject IUserSession UserSession
@inject IOptions<AppSettings> AppSettings

@if (UserSession.IsAuthenticated)
{
@(Html.Kendo().Window()
.Name("SessionExpireNotification")
.Title("Need More Time?")
.Modal(true)
.Content(@<text>
<p>
Your session is about to expire. You will be automatically signed out in
</p>
<h2 style="margin-top: 0">
<span id="logout-counter-span">0@(AppSettings.Value.CookieAuthentication.SessionExpireNotificationMinutes):00</span>
</h2>
<p>
To continue your session, select <strong>Stay Signed In</strong>.
</p>
<p>
<button id="stay-logged-in-button" type="button" class="btn btn-primary">
Stay Signed In
</button>
<button id="signout-button" type="button" class="btn btn-default">
Sign out
</button>
</p>
</text>)
.Width(450)
.Visible(false)
.Events(ev => ev.Close("onSessionExpireNotificationClose"))
)

<script>

var notificationInterval,
logoutInterval,
logoutCounterSpan;

function startNotificationCounter() {
var counter = @AppSettings.Value.CookieAuthentication.ExpireMinutes;
notificationInterval = setInterval(function() {
counter--;
if (counter === @AppSettings.Value.CookieAuthentication.SessionExpireNotificationMinutes) {
$("#SessionExpireNotification").data("kendoWindow").center().open();
startLogoutCounter();
}
},
60000);
}

function startLogoutCounter() {
var counter = @(AppSettings.Value.CookieAuthentication.SessionExpireNotificationMinutes*60);
logoutInterval = setInterval(function() {
counter--;
if (counter < 0) {
$("#logoutForm").submit();
} else {
var m = Math.floor(counter / 60);
var s = Math.floor(counter % 60);
var mDisplay = m < 10 ? "0" + m : m;
var sDisplay = s < 10 ? "0" + s : s;
logoutCounterSpan.text(mDisplay + ":" + sDisplay);
}
},
1000);
}

function resetCounters() {
clearInterval(notificationInterval);
clearInterval(logoutInterval);
logoutCounterSpan.text("0@(AppSettings.Value.CookieAuthentication.SessionExpireNotificationMinutes):00");
startNotificationCounter();
}

function onSessionExpireNotificationClose() {
resetCounters();
}

$(function() {
logoutCounterSpan = $("#logout-counter-span");

startNotificationCounter();

$("#stay-logged-in-button").click(function() {
$.get("@Url.Action("Index", "KeepAlive", new {area = ""})",
null,
function(data) {
resetCounters();
$("#SessionExpireNotification").data("kendoWindow").center().close();
}
);
});

$("#signout-button").click(function() {
$("#logoutForm").submit();
});
});

</script>
}

延长 session 超时很容易。您只需调用一个虚拟操作方法。

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Asp.Web.Controllers
{
[AllowAnonymous]
public class KeepAliveController : Controller
{
//
// GET: /KeepAlive
[AllowAnonymous]
public ActionResult Index()
{
return Content("I am alive!");
}
}
}

关于asp.net-core - 在 ASP.NET Core 中 session 过期之前显示 session 超时警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45764584/

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