1

I am trying to get my head around MVC 5 Web Application template, and I noticed that special attention is given to the security around the LogOff link.

In the scaffold template the "LogOff" link in the _LoginPartial.cshtml view sits inside an HTML form with an AntiForgeryToken in it, and is defined as a JS call to form's submit action, like so:

@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}

With the corresponding action method Account/LogOff inside ActionController defined like so:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

My question is - what is the reasoning behind it? Why does the LogOff action require so much security protection? Why not just have this in the view,

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
@Html.ActionLink("Log Off", "LogOff", "Account", routeValues: null, htmlAttributes: new { title = "LogOff" })

And this in the controller:

 public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

What security hole would this create?

Thanks.

Paceman
  • 2,095
  • 4
  • 20
  • 27

1 Answers1

1

Please refer to this link: Logout: GET or POST?.

It will answer your question on why Post should be used in logout.

Community
  • 1
  • 1
Hezye
  • 1,521
  • 1
  • 13
  • 15