gpt4 book ai didi

c# - CookieAuthenticationOptions.LogoutPath 属性在 ASP.NET Core 2.1 中有什么作用?

转载 作者:行者123 更新时间:2023-11-30 20:15:44 27 4
gpt4 key购买 nike

在 ASP.NET Core 2.1 中,谁能解释一下 CookieAuthenticationOptions.LogoutPath 的作用?每the documentation它说:

If the LogoutPath is provided the handler then a request to that path will redirect based on the ReturnUrlParameter.

但我什至不认为那句话有正确的语法,所以我对意思感到困惑。

Startup.cs中,我是这样设置的:

// Added after AddMvc()
services.ConfigureApplicationCookie(options =>
{
options.LogoutPath = $"/account/logout";
});
  1. 什么时候调用?

  2. 我是否需要为此在我的 AccountController 和 View 中创建相应的 GET 操作?或者 POST 操作会起作用吗?例如:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Logout()
    {
    await _signInManager.SignOutAsync();
    return RedirectToAction("Index", "Home");
    }
  3. 我的注销操作是否需要将用户注销,或者到那时他们已经注销了吗?

最佳答案

LogoutPath您可以配置 cookie 身份验证方案是一个奇怪的方案。而 LoginPath具有直接影响,基本上是当 cookie 身份验证受到挑战时最终用户被重定向到的 URL,LogoutPath不直接使用。

相反,配置的 LogoutPath仅用于在使用 cookie 身份验证方案发生注销时验证当前 URL。 check looks like this :

// Only redirect on the logout path
var shouldRedirect = Options.LogoutPath.HasValue && OriginalPath == Options.LogoutPath;
await ApplyHeaders(shouldRedirect, context.Properties);

所以这基本上检查了 OriginalPath 是否当前请求的路径等于配置的注销路径。如果是这样的话,那么 ApplyHeaders调用将执行重定向到 RedirectUri身份验证属性。

这样做的目的是确保重定向回路径只能在访问真实注销 URL 时发生。因此,例如,如果用户单击注销按钮,他们可能会被注销,然后被重定向回他们来自的地方。但是,如果他们在别处注销,则他们不会自动重定向回来,因为只有注销 URL 被认为是将用户重定向回来的安全位置。

LoginPath 存在相同的逻辑太顺便说一句。但还有一个额外的逻辑,即当该方案受到挑战时(例如,当需要通过授权过滤器进行身份验证时),cookie 身份验证方案还将重定向到该 URL。

Do I need to create a corresponding GET action in my AccountController and View for this? Or will a POST action work?

这取决于您以及您希望如何处理注销。要运行上述逻辑,您只需要在该路由上执行任何操作,因此您还可以执行 POST 要求用户执行表单提交以便注销(以防止意外注销通过 GET 请求)。

Does my Logout action need to sign the user out or will they have already been signed out by that point?

您必须调用 SignOutAsync你自己,因为这些路由没有隐式处理。就像你也需要在LoginPath上实现自己的登录逻辑一样,您还需要实现注销逻辑。

配置的路径实际上只是为了让 cookie 方案知道这些路由在哪里,但它们不会对其行为产生任何影响。

关于c# - CookieAuthenticationOptions.LogoutPath 属性在 ASP.NET Core 2.1 中有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com