I would like to have a logout action in my controller. I don't want to get a new page to appear. I just want to send a message to the controller. Here's what I was thinking.
But I have some questions.
a) Should it be a "get" or a "post" b) To completely clear what's in the browser should I redirect to a new page also?
[HttpPost]
public JsonResult JsonLogout()
{
FormsAuthentication.SignOut();
return Json(new { success = true });
}
Since you have the HttpPost attribute attached to your action you will need to send your ajax request with POST as well. If you remove this attribute you will be able to use both POST and GET and both would work fine for this application.
You can send this action at any time and without a page refresh if you would like, however, you should consider that if the only thing you do is send the ajax request and do not change the page there will be no feedback to the user that they are actually logged out. You could popup a message but other elements of the page would still be shown. The only feedback they would receive is when they click on another link and realize it is asking them to log back in.
EDIT: Also, my feedback on this would be to not use ajax at all. You would be much better have having the page reload to ensure you have a clean, non authenticated, interface.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With