4

我正在尝试检查服务器是否已通过身份验证超时,然后Request.IsAuthenticated = false我是否希望将用户重定向到 LoGon 页面。但是,即使身份验证的时间已经过期,它总是给我Request.IsAuthenticated = true,但是当应用程序第一次启动时就可以了Request.IsAuthenticated = false

我无法检查会话超时,因为主要页面永久从服务器获取数据,我认为会话永远不会超时。

在 WebConfig 中

<code>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="1" />
    </authentication>
</code>

在服务器上:

<code>
public class CheckAuthorizeAndSessionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;

            //  check if session is supported
            if (ctx.Request.IsAuthenticated)
            {
                if (ctx.Session != null)
                {
                    // check if a new session id was generated
                    if (ctx.Session.IsNewSession)
                    {

                        // If it says it is a new session, but an existing cookie exists, then it must
                        // have timed out
                        string sessionCookie = ctx.Request.Headers["Cookie"];
                        if (null != sessionCookie)
                        {
                            FormsAuthentication.SignOut();
                            //const string loginUrl = System.Web.Security.FormsAuthentication.LoginUrl;// Url.Action("LogOn", "Account");
                            //var rr = new RedirectResult(loginUrl);
                            //filterContext.Result = rr;
                            String url = FormsAuthentication.LoginUrl;
                            filterContext.Result = new RedirectResult(url);
                        }
                    }
                }
            }
            else
            {
                ctx.Response.Redirect(@"~/Account/LogOn");
                //ctx.Response.StatusCode = 302;
            }
            base.OnActionExecuting(filterContext);
        }
    }
</code>

在客户端

<code>
$(document).ready(function () {
        //DELETE
        $("#ModifyBlock a").live("click", function () {
            var urlForGet = '';
            var urlAction = '';
            if ($(this).attr("id") == 'Delete') {
                urlForGet = '@Url.Action("Delete", "Product")';
                urlAction = '@Url.Action("Delete", "Product", new { id = "idClient", lockType = "typeLockClient" })';
            }
            if ($(this).attr("id") == 'Edit') {
                urlForGet = '@Url.Action("Edit", "Product")';
                urlAction = '@Url.Action("Edit", "Product", new { id = "idClient", lockType = "typeLockClient" })';
            }
            if ($(this).attr("id") == 'Detail') {
                urlForGet = '@Url.Action("Detail", "Product")';
                urlAction = '@Url.Action("Detail", "Product", new { id = "idClient", lockType = "typeLockClient" })';
            }
            $.ajax({
                url: urlForGet,
                type: 'GET',
                data: { id: $(this).attr("alt"), lockType: $("#SelTypesLock").attr("value") },
                dataType: 'json',
                proccessData: false,
                contentType: 'application/json; charset=utf-8',
                statusCode: {
                    200: function (data) {
                        alert('200: Authenticated');
                    },
                    401: function (data) {
                        alert('401: Unauthenticated');
                    },
                    550: function (data) {
                        alert('550: Unauthenticated');
                        $("#ErrorMesage").text(xhr.responseText);
                    },
                    660: function (data) {
                        alert('660: Redirect to Error View');
                        window.location.href = '@Url.Action("Error", "Product")';
                    }
                },
                success: function (data) {
                    url = urlAction;
                    url = url.replace("idClient", data.Id);
                    url = url.replace("typeLockClient", $("#SelTypesLock").attr("value"));
                    window.location.href = url;
                },
                error: function (xmlHttpRequest, status, err) {
                    $("#ErrorMesage").text(xmlHttpRequest.responseText);
                }
            });
        });
    </code>`enter code here
4

1 回答 1

1

您可能将滑动过期参数设置为 true。这样做是根据 web.config 中的参数测量最后一次请求的时间。

如果您的参数是 1 分钟,并且您执行 30 秒的 Ajax 调用,那么您将永远不会被取消身份验证。尝试关闭滑动到期,应该可以工作

于 2012-12-24T07:22:29.147 回答