MVC5 Log Off link fails from different Areas

2.1k views Asked by At

I have an MVC5 application with a number of different Areas. The project was created with a Log Off link in the navbar, but if the user is in any of the Areas, the link is broken. I thought adding:

new { area = "" }

would point the link back to the right place but either it doesn't or I tried it in the wrong place. Code as below:

    @using Microsoft.AspNet.Identity
    @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(), "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
              </li>
              <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
            </ul>
        }
     }
     else
     {
        <ul class="nav navbar-nav navbar-right">
            <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
        </ul>
     }

Any ideas on what needs to be changed to have the Log Off work regardless of what Area the user is currently in?

1

There are 1 answers

2
thmshd On

The right place is within the Html.BeginForm(...) parameters:

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