【问题标题】:Form Authentication redirecting for Ajax submitAjax 提交的表单身份验证重定向
【发布时间】:2014-06-04 12:53:15
【问题描述】:

我正在开发 MVC5 网络应用程序。有两种观点-

  1. 首页
  2. 登录

当用户尝试访问主页视图时,他或她被重定向到登录页面。因为各个控制器都装饰有 Authorize 属性。

登录视图的 AjaxForm 为 -

@{
var ajaxOptions = new AjaxOptions
                      {
                          HttpMethod = "POST",
                          InsertionMode = InsertionMode.Replace,                              
                          UpdateTargetId = "content",
                          LoadingElementId = "loader",
                          OnSuccess = "AjaxSuccess",
                          OnFailure = "AjaxError"
                      };
}

@using (Ajax.BeginForm("Authenticate", "Login", ajaxOptions, new { id = "LoginForm", @class = "login-form" }))
{
    @Html.Raw(TempData["ErrorMessage"])
    ---Some code continues---

现在用户填写他/她的凭据。请求转到 Authenticate 以检查凭据。现在,如果出现一些错误,我将表格重新显示为 -

        [HttpPost]
        [AllowAnonymous]
        [ActionName("Authenticate")]
        public ActionResult Index(LoginModel model)
        {
            try
            {
                // Some code for validation and redirecting by use of if
                // But credentials not matched so show error    
                TempData["ErrorMessage"] = string.Format(Login.ErrorDiv, "Incorrect username or password.");
            }
            catch (Exception exc)
            {
                TempData["ErrorMessage"] = string.Format(Login.ErrorDiv, exc.Message);
            }

            // If we got this far, something failed, redisplay form
            return PartialView("_Login", model);
        }

现在按照预期,登录表单即登录部分视图)应该重新显示错误。它没有发生。而是调用登录表单的获取请求。并且整个表单(或整个视图)显示在 UpdateTargetId 中。

当我从 Fiddler 检查请求时。请求进行身份验证。但随后表单被重定向回

http://localhost/nsu/Login/Index?ReturnUrl=%2fnsu%2fLogin%2fAuthenticate

我正在寻求帮助来解决这个奇怪的问题,并继续使用 AjaxForm 登录。

【问题讨论】:

    标签: jquery ajax model-view-controller


    【解决方案1】:

    这个问题的答案是在web.config中正确配置Form Authentication的设置。

    将 web.config 配置为 -

        <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880" />
        </authentication>
    

    其中 loginUrl 是登录视图的路径。动作名称是Login,控制器是AccountController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多