Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC5 Log Off link fails from different Areas

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?

like image 267
user3632714 Avatar asked Nov 18 '14 12:11

user3632714


1 Answers

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

using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id =  "logoutForm", @class = "navbar-right" }))
like image 125
thmshd Avatar answered Nov 03 '22 14:11

thmshd