Question

I have a Controller method with the following code:

ClearAuthenticationCookies();
FormsAuthentication.SetAuthCookie(EmailAddress, false);

RedirectToAction("Index", "Home");

var responseObject = new responseObject
{
    responseCode = "2",
    responseMessage = "No"
};
return Json(new {responseObject = responseObject});

This is executed from another website via ajax, but doesn't show the proper url in the browser. If I login directly, it works:

 $.ajax({
        type: "POST",
        url: "http://localhost:51437/UserAccount/SignOn2",
        dataType: 'json',
        data: jsonData,
        crossDomain: true,
        success: function(result) {
        },
        error: function(result) {
        }
    });

I also have to click my link twice before it actually logs me in, but the page is not found.

Was it helpful?

Solution

Your controller signature should been public RedirectResult SignOn(...) and return RedirectToAction("Index", "Home"); is the object that you should return from your controller. Don't return any JsonResult after this.

UPDATE: You can return one response over one request. To achieve what you want, you have two options. First: return json result, then when client receive json result and verify it you can send another request to server and and than redirect to another page. Or you can redirect from the client, using window.location.replace to change current page via js. Furthemore, you can prepare link on the server to achieve all .net routes advantage and include it to the json result and than make something like this:

$.ajax {
  ..
  ..
  ..
  success: function(data) {
    alert(data.message_for_user);
    window.location.replace(data.url_to_redirect);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top