In my MVC3 project, I have a controller with an [Authorize] attribute. I have a form submission without ajax, which redirects the user (as expected) to the login screen, if he/she is not logged in.
However, now I have a form which is submitted with jquery ajax, and how can I do the same thing? Redirect the user to the login screen, if he/she is not authorized? After a successful login, the user should is redirected to the initial action.
Controller
[Authorize]
[ValidateInput(false)]
public JsonResult SubmitChatMessage(string message)
{
if (!string.IsNullOrEmpty(message))
{
// Do stuff
}
// Return all chat messages
return GetChatMessages();
}
Client JQUERY
$(document).ready(function () {
$("form[action$='SubmitChatMessage']").submit(function (event) {
$.ajax({
url: $(this).attr("action"),
type: "post",
dataType: "json",
data: $(this).serialize(),
success: function (response) {
// do stuff
}
});
return false;
});
});
I can see from firebug console window, that the server returns:
GET http://domain/Account/LogOn?ReturnUrl=%2fProductDetails%2fSubmitChatMessage
Looking forward to your help!
UPDATED with possible solutions