Redirección de autenticación de formulario para envío Ajax

I'm working on MVC5 web application. There are two views -

  1. Home
  2. Iniciar sesión

When user tries to access Home view, he or she is redirected to Login page. Because the respective controller is decorated with Autorizar atributo.

The Login view is having AjaxForm as -

@{
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---

Now user fill his/her credentials. The request goes to Autenticar for checking credentials. Now if some error arise, I am reshowing form as -

        [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);
        }

Now as per expectations, the Ingresa forma (i.e. Login partial view) should have redisplayed with error. It's not happening. Instead the Get request for Login form is called. And whole form (or whole View) is displayed in ActualizarTargetId.

While I checked from Fiddler for the request. The request is made to Authenticate. But then form is redirected back to

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

I'm looking for help to fix this weird issue with keep using AjaxForm for Login.

preguntado el 04 de junio de 14 a las 13:06

1 Respuestas

The answer to this question is to configure Form Authentication's setting in web.config correctamente.

Configure web.config as -

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

where loginUrl is path to Login view. The action name is Iniciar sesión y el controlador es Controlador de cuenta.

Respondido el 08 de junio de 14 a las 00:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.