10

I have implemented authentication in Asp.Net Core 2.2 like this:

public async Task<IActionResult> LoginAsync(string user, string password)
    {
        if (user == "admin" && password == "admin")
        {
            var claims = new[] { new Claim(ClaimTypes.Name, user),
            new Claim(ClaimTypes.Role, "Admin") };

            var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(identity));

            return RedirectToAction("Index", "Home");
        {
        else
        {
            return RedirectToAction("Login", "Users");
        }

I need to make a Logout action now. I used to achieve this in Asp.Net MVC with FormsAuthentication.SignOut()... I need to know the proper way to do it in Asp.Net Core 2.2

What I've tried is to make a Logout action like this:

    public async Task<IActionResult> Logout()
    {
        await HttpContext.SignOutAsync();
        return RedirectToAction("Index","Home");
    }

And used the following code in my NavBar:

@if (User.Identity.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.Name + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
                        </li>
                        <li class="nav-item">
                            <form class="form-inline" asp-area="Identity" asp-page="/Users/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
                                <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
                            </form>
                        </li>
                    </ul>
                }
            }
            else
            {
                <ul class="nav navbar-nav navbar-right">
                    <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
                    <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
                </ul>
            }

Following instructions from this documentaion

This correctly shows the Logout button, but pressing the button doesn't seem to trigger my action, and the user is not logged out.

5
  • 3
    Hi there! Welcome to stackoverflow. What have you try so far? What does the docs says about this? Commented May 14, 2019 at 3:07
  • @vasily.sib I tried making a Logout action with HttpContext.SignOutAsync(), and then proceeded to make a form with an asp-page="Users/Logout" tag and a submit button inside, but after clicking the button, nothing happens... My button doesn't seems to trigger my action. Commented May 14, 2019 at 3:18
  • 2
    Then you should edit your question to show that. Show us your Logout action (with all attributes that you apply to it) and a form that you use to trigger that action Commented May 14, 2019 at 3:20
  • 2
    Have you try to debug it? Do you actually hit your Logout action? If you open developer console in your browser what response from server do you receive on that button click? Commented May 14, 2019 at 3:50
  • I wasn't hitting my action after all... Silly mistakes were made. Thanks for your help Vasily. Commented May 14, 2019 at 3:52

2 Answers 2

11

Turns out I was simply making a mistake in my View. I was calling the wrong action in my form.

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

Should've been,Html.BeginForm("Logout","Users", ...)

Also, my form was sending a Post request, so my action had to be decorated with [HttpPost], like this:

[HttpPost]
public async Task<IActionResult> Logout()
{
    await HttpContext.SignOutAsync();
    return RedirectToAction("Index","Home");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why but HttpPost didn't work for me. I changed it to HttpGet and it worked!
2

You can do something like this if you want to use an a tag:

<form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
    <a href="javascript:;" onclick="parentNode.submit();">
        <span>Logout</span>
    </a>
</form>

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.