使用Asp.Net Core Cookie身份验证进行注销操作

9
我已经按照以下方式在Asp.Net Core 2.2中实现了身份验证:
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");
        }

现在我需要进行注销操作。在Asp.Net MVC中,我曾使用FormsAuthentication.SignOut()来实现此操作... 现在我需要知道在Asp.Net Core 2.2中正确的做法。

我尝试创建以下Logout action:

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

我在我的导航栏中使用了以下代码:

@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>
            }

根据文档的指示:

虽然正确显示了“登出”按钮,但按下该按钮似乎没有触发我的操作,用户也没有成功退出登录。


3
你好!欢迎来到stackoverflow。你尝试过什么了吗?文档对此有什么说法? - vasily.sib
@vasily.sib 我尝试使用HttpContext.SignOutAsync()创建一个注销操作,然后创建了一个带有asp-page="Users/Logout"标签和提交按钮的表单,但是在单击按钮后,什么也没有发生...我的按钮似乎无法触发我的操作。 - Cristopher Rosales
2
那么你应该编辑你的问题来展示它。展示你的“注销”操作(包括你应用的所有属性)和一个用于触发该操作的表单。 - vasily.sib
2
你尝试过调试吗?你是否真正触发了“注销”操作?如果在浏览器中打开开发者控制台,你在单击该按钮时从服务器收到了什么响应? - vasily.sib
我最终没有触发我的动作...犯了一些愚蠢的错误。谢谢你的帮助,Vasily。 - Cristopher Rosales
2个回答

10

结果证明我在视图中只是犯了一个错误。我在表单中调用了错误的操作。

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

应该改为:Html.BeginForm("Logout","Users", ...)

此外,我的表单发送了一个Post请求,所以我的操作必须使用[HttpPost]进行装饰,就像这样:

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

我不知道为什么,但 HttpPost 对我没用。我把它改成了 HttpGet,然后就可以了! - Karishma

0
如果您想使用 a 标签,可以像这样做:
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接