【问题标题】:ASP.NET Core MVC calling async method from cshtml page [duplicate]ASP.NET Core MVC从cshtml页面调用异步方法[重复]
【发布时间】:2021-06-09 19:41:22
【问题描述】:

我正在制作一个网站来简单地管理,但我无法将用户注销,我正在使用 cookie 来验证用户。当我尝试异步注销用户时,它说“找不到页面”。这是我想出的:

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

在我的 .cshtml 文件中:

@if (User.Identity.IsAuthenticated)
{
    <p>Hello, @User.Identity.Name</p>
    <a onclick="document.getElementById('logout_form').submit();" style="cursor: pointer;">
        Sign out
    </a>
    <form asp-controller="Home" asp-action="SignOutAsync" method="post" id="logout_form"></form>
}
else
{
    <a asp-controller="Home" asp-action="Login">Sign in</a>
}

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    试试这个?页面未找到,因为需要在方法中添加重定向。

    您是否需要登出帖子? get 还不够吗?

    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties
            { 
                RedirectUri = Url.Action("Index","Home")
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多