Bil*_*ara 7 ajax asp.net-mvc-5
我正在使用cookie身份验证MVC5.我的网页每隔1-5秒就严重依赖经过身份验证和未经身份验证的Ajax调用来保持数据更新.因此,我的用户永远不会退出该网站.
我的理想情况:如果用户正在我的网站上主动浏览或执行操作,请保持会话处于活动状态.如果他们在10分钟后打开了一个页面,我希望他们的会话超时,我将使用失败的Ajax调用重定向到登录页面.我认为最好在控制器或动作级别完成.
我尝试按照下面的建议控制会话状态行为,但会话仍然没有超时.在每秒点击ReadOnly/Public一次65秒后,我调用ReadOnly/Authorized并成功从中检索数据.
这是我的CookieAuthentication配置.
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromMinutes(1),
});
}
Run Code Online (Sandbox Code Playgroud)
我的测试页面:
<div id="public"></div>
<div id="authorized"></div>
@section scripts{
<script>
function poll(times) {
var url = '/ReadOnly/Public';
$.ajax({
url: url,
dataType: 'json',
data: null,
cache: false,
success: function (data) {
$('#public').html(times + ' ' + data.test);
},
error: function (data) {
$('#public').html(times + ' ' + 'failed');
}
});
};
function checkAuth(times) {
var url = '/ReadOnly/Authorized';
$.ajax({
url: url,
dataType: 'json',
data: null,
cache: false,
success: function (data) {
$('#authorized').html(times + ' ' + data.test);
},
error: function (data) {
$('#authorized').html(times + ' ' + 'failed');
}
});
};
$(function () {
var times = 1;
setInterval(function () {
poll(times);
times++;
}, 1000);
setInterval(function () {
checkAuth(times);
}, 65000);
});
</script>
}
Run Code Online (Sandbox Code Playgroud)
和测试控制器代码(尝试使用disabled和readonly选项)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
namespace SessionTest.Controllers
{
[SessionState(SessionStateBehavior.ReadOnly)]
public class ReadOnlyController : Controller
{
[Authorize]
public ActionResult Authorized()
{
return Json(new
{
test = "ReadOnly and Authorized"
}, JsonRequestBehavior.AllowGet);
}
public ActionResult Public()
{
return Json(new
{
test = "ReadOnly and Public"
}, JsonRequestBehavior.AllowGet);
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许您需要有 2 个独立的网络应用程序。一种是用于服务经过身份验证的请求。另一项则适用于所有公众请求。
这类似于 Google Analytics 脚本如何在 Google 端创建和维护有关您网站的自己的会话,而不会影响您的 Web 应用程序的内部会话管理。否则,您将陷入 ASP .NET 处理 cookie 和保持会话活动状态的默认行为。
祝你好运。
| 归档时间: |
|
| 查看次数: |
2626 次 |
| 最近记录: |