1

In Microsoft ASP.NET MVC 5,

how do I sign an user out from an action other than the LogOff action of the Account controller? Some years ago I would use return RedirectToAction("LogOff","Account"), but nowadays it does not work anymore since LogOff is a Post action (not GET).

public ActionResult SomeActionOfSomeController() {

 // some logic
 return RedirectToAction("LogOut", "Account"); //does not work since LogOut has HttpPost attribute

}
TapiocaCom
  • 353
  • 5
  • 16
  • I think you are looking for some what similar to Server.Transfer in ASP.net mvc. Right ? – dotnetstep Jun 20 '14 at 02:00
  • Based on your question the GET method no longer works on MVC 5!!, This is not right. – Ali Jun 20 '14 at 02:51
  • the question is clear enough for the user dotnetstep, for example... There is no assertion saying explicitly that "HttpGet does not work". Actually, what I am saying is that RedirectToAction("LogOff","Account") does not work anymore since LogOff is a Post action (not GET). – TapiocaCom Jun 20 '14 at 03:09

4 Answers4

1

This is the way I do it and it's using GET method, Is this what are you asking for ?

    [HttpGet]
    public ActionResult Logout()
    {
        WebSecurity.Logout();

        // instead of displaying logout page directly we redirect to confirmation page.
        // this will ensure auth cookie is cleared, which, in turn, ensures correct menu items are displayed

        return RedirectToAction("LogoutConfirm");
    }

    [HttpGet]
    public ActionResult LogoutConfirm()
    {
        return View();
    }
Ali
  • 2,574
  • 1
  • 17
  • 24
  • this has nothing to do with my question... Furthermore, LogOut Actions should be HttpPost; you are doing it wrong. – TapiocaCom Jun 20 '14 at 01:48
  • This is currently in used on an **UP** and **RUNNING** application and it works absolutely with no issue, there is nothing to do with POST method in here, unless you want to use it with POST method. You may need to explain a little more about the question as it seems a bit ambiguous. – Ali Jun 20 '14 at 02:09
  • First of all: your Logout Action should be POST (not GET). It is a general recomendation. Second: I am not asking how to make a log-out action... this is trivial. My question is how to redirect to a logout Action from another action... – TapiocaCom Jun 20 '14 at 02:15
  • Please explain to us then we can learn, Why we should use POST instead of GET (why this is critical as a general recommendation)? and as I can see in the code above clearly there is a redirection from `Logout`action to the `LogoutConfirm` action ? – Ali Jun 20 '14 at 02:30
  • Then you should reconsider your main question, Based on your question _"but nowadays it does not work anymore since LogOff is a Post action (not GET)"_ this tells people that HttpGet is no longer work (outdated) on MVC 5. You must be clear when you ask a question. this doesn't mean the GET method is not working anymore – Ali Jun 20 '14 at 02:49
  • the question is clear enough for the user dotnetstep, for example... There is no assertion saying explicitly that "HttpGet does not work". Actually, what I am saying is that RedirectToAction("LogOff","Account") does not work anymore since LogOff is a Post action (not GET). – TapiocaCom Jun 20 '14 at 03:12
0

You can do something like this right?

@using (Html.BeginForm("LogOff", "Account"))
{
        @Html.AntiForgeryToken()
        <button type="submit">Logout</button>
}

And you can do this, if you want a link, instead of button

@using (Html.BeginForm("LogOff", "Account",
        FormMethod.Post, new { id = "LogOffForm" }))
{
        @Html.AntiForgeryToken()
        <a href="@Url.Action("LogOff", "Account")"
            onclick="$('#LogOffForm').submit();">Logout</a>
}
Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • 2
    OP is asking how to perform a LogOff from an MVC Controller action, not how to submit a request to LogOff from the rendered view. – damienc88 Jun 20 '14 at 00:46
0

Is your SomeActionOfSomeController present in Account Controller or some other other controller ?

If it is present in AccountController then you can do following thing instead of calling redicttoaction.

    public ActionResult Index2()
    {            
        return Index3();
    }

    [HttpPost]
    public ActionResult Index3()
    {
        return Content("Test");
    }

If it is from other controller action then you have to create object of AccountController and then call Method.

Hope this will help you.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72
0

Assuming you are implementing the default ASP.net login / logout, you can paste the following code in your Controller.

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
return RedirectToAction("Index", "Home");

Note: These two lines are the same code that are present in Account/Logoff Controller Action method.