1

I have this html in my asp.net MVC 4 application:

 @if (User.Identity.IsAuthenticated)
                 <li class="login-link">     {
  @Html.ActionLink(User.Identity.Name, "LogOff", "Account")
 </li>
}

and this controller action

//[HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    WebSecurity.Logout();

    return RedirectToAction("Index", "Home");
}

but when i click log out link, user doesnt log out.

Please suggest how to fix it

5
  • Could you paste the link generated by Html.actionlink() in the browser? Commented May 5, 2014 at 11:10
  • <a href="/Account/LogOff">[email protected]</a> Commented May 5, 2014 at 11:12
  • <li class="login-link"> <a href="/Account/LogOff">My Name</a> </li> Commented May 5, 2014 at 11:13
  • Nothing wrong with you code. I could suggest to debug so you will see what happens. Put a breakpoint in the action, Commented May 5, 2014 at 11:23
  • 1
    Have look in the link possible duplicate Commented May 5, 2014 at 11:30

4 Answers 4

2

In your code sample above, you wrote:

 @if (User.Identity.IsAuthenticated)
                 <li class="login-link">     {
  @Html.ActionLink(User.Identity.Name, "LogOff", "Account")
 </li>
}

Based on how your @if (User.Identity.IsAuthenticated) statement resolves this may lead to some wonky code being rendered for the browser.

Does the following Razor code more correctly reflect your intent?

@if (User.Identity.IsAuthenticated)
{
    <li class="login-link">
        @Html.ActionLink(User.Identity.Name, "LogOff", "Account")
    </li>
}
Sign up to request clarification or add additional context in comments.

Comments

0

I couldn't find anything wrong with your code. But try this and let me know if it works for you:

cshtml :

@Html.ActionLink("Log Off", "LogOff", "LogOn", null, new { @class = "actnclass" })

controller :

public ActionResult LogOff()
{
    Request.Cookies.Remove("UserId");
    FormsAuthentication.SignOut();
    return RedirectToAction("LogOn", "LogOn");
}

1 Comment

If you can't find anything wrong with the code someone posts, please do not just copy-paste code of your own. The other answer points out some specific issues with the code.
0

Your Razor code looks good. Even a basic html logout code like the one below should do

<a href="@Href("~/Account/LogOff")">Logout</a>

Keep a breakpoint in your LogOff action in your Account controller and see what is happening. You seem to be using WebSecurity.Logout(); to logout and then redirect the user to the Home/Index page.

Also would like to know by looking at what are you inferring that user has not logged out.

Comments

0

Nothing wrong with you code. The point is the Action LogOff in Account is HttpPost Method, so it will only accept from a form submit.

You have to 3 ways to achieve Log off functionality

  1. Change LogOff Method to HttpGet instead of HttpPost in Account Controller
  2. Add an HttpGet overload for it take one parameter to leave the HttpPost method in Account Controller

    [HttpGet]
    public ActionResult LogOff(string token) { 
         AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
         return RedirectToAction("Index", "Home");
    }
    
    // Already Exists
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff() { 
         AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
         return RedirectToAction("Index", "Home");
    }
    
  3. Use Javascript to submit the LogOff form

    @using Microsoft.AspNet.Identity
    @if (Request.IsAuthenticated)
    {
        using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
        {
             @Html.AntiForgeryToken()
             // ...
             <a href="javascript:document.getElementById('logoutForm').submit()">LogOff</a>
        }
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.