【问题标题】:Ajax request after MVC authentication timeout/session cookie deletedMVC 身份验证超时/会话 cookie 删除后的 Ajax 请求
【发布时间】:2015-11-22 23:04:42
【问题描述】:

当用户超时或会话 cookie cookie 被删除时使用 MVC 5 身份验证,用户将被重定向到登录页面。

这在整个页面加载时工作正常,但是对于我的 Ajax 调用,这会破坏客户端。重定向在身份验证页面显示时起作用,但没有响应(在标准加载轮之后)。

我很确定这是某种 JS 问题。

JS:

$(function () {
    $('#dealSearchForm').on("submit", function (e) {
        e.preventDefault();
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (data) {
                $('#dealSearchForm').html(data);
            },
            complete: function (data) {
                $('#searchResults').show();
            }
        });
    });
});

主视图:

@{Html.RenderPartial("_DealSearch");}

<script src="@Url.Content("~/Scripts/DealSearch.js")"></script>

<script>

    $(document).ready(function () {
        @if (Model.Deals.Count > 0) {
            @Html.Raw("$('#searchResults').show();");
        }
    });

</script>

局部视图:

@using (Html.BeginForm("DealSearch", DealManagement, FormMethod.Post, new { @id = "dealSearchForm" }))
{
/* Search filters etc */

<div class="col-xs-7">
    <button id="button-search" class="btn btn-primary pull-right" type="submit" data-loading-text="Please wait...">Search</button>
</div>

<div id="searchResults">
    /* Grid with results */
</div>

控制器:

public virtual ActionResult Index()
        {
            var model = new DealSearchModel();

            return this.View(model);
        }

public virtual ActionResult DealSearch(DealSearchModel model)
        {
            // Get deals from service, uses search criteria
            model.Deals = GetDeals(model);

            // Return the view
            return PartialView(MVC.DealManagement.Views._DealSearch, model);
        }

客户端出现错误:

如果有人有任何想法,他们将不胜感激!

【问题讨论】:

  • 你还能显示服务器端(控制器)代码吗?
  • 嗨,我已经添加了控制器。谢谢。
  • 在 JS 中试试这个剃须刀代码:stackoverflow.com/questions/5614941/…
  • 你能检查服务器端的 SessionId 或 Cookie 值吗?如果不存在重定向到登录页面

标签: ajax asp.net-mvc


【解决方案1】:

使用基于 Kamos 答案的方法,我使用以下方法解决了这个问题:

自定义属性

public class AuthoriseAjaxAttribute : AuthorizeAttribute 
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.Result = new RedirectResult("~/Error/AjaxUnauthorized");
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}

/Error/AjaxUnauthorised 视图

@{
    Layout = ""; // Result of an ajax call
}

// Refresh the page, resulting in MVC default login/redirection
<script>
    $(document).ready(function($) {
        location.reload(true);
    });
</script>

// Only here for if Javascript is off
<div class="panel panel-default">
    <div class="panel-heading">
        <div class="row">
            <div class="col-xs-12">
                Authorisation timeout.
            </div>
        </div>
    </div>
    <div class="panel-body black-text">
        Please refresh the page and log back in.
    </div>
</div>

对于用户来说,这使得 Ajax 调用以同样的方式响应超时。

【讨论】:

    【解决方案2】:

    您必须覆盖默认行为 - 我假设您的问题是当用户点击需要授权且当前会话已过期的操作时,AJAX 响应不再有效。

    您没有提供如何授权您的方法,但考虑创建自定义授权属性:

    public class CustomAuthorizationAttribute : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            // Check whether it is an AJAX or standard request
        }
    }
    

    如何判断是不是AJAX请求可以找到here。您应该返回 403 未经授权的结果并在您的 AJAX 调用中处理它,而不是重定向。

    【讨论】:

      猜你喜欢
      • 2016-06-23
      • 2010-10-02
      • 2016-05-30
      • 2011-03-07
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 2017-04-28
      • 2016-03-05
      相关资源
      最近更新 更多