注销后重定向在 Asp.net Core 2 中不起作用

Redirect after signout not working in Asp.net Core 2

我正在使用 Asp.net Core 2.2 和 AzureAD 身份验证。它工作正常,但现在我在尝试实现注销时遇到了麻烦 url。

我在控制器中尝试了以下操作:

[HttpGet("[action]")]
public IActionResult SignOut()
{
    return SignOut(new AuthenticationProperties { RedirectUri = Url.Action(nameof(AfterSignOut)) }, AzureADDefaults.AuthenticationScheme);
}

[HttpGet("[action]")]
[AllowAnonymous]
public IActionResult AfterSignOut()
{
    return Ok("It's working!");
}

当我使用浏览器进入 https://mySite/myController/SignOut 时,注销操作正常(我的用户已注销,下次我转到某个页面时,我必须重新登录)

但是,问题是 我没有重定向https://mySite/myController/AfterSignOut url,如 AuthenticationProperties 中指定的那样。相反,/SignOut 只是 returns HTTP 代码 200,仅此而已,它不会将我重定向到任何地方。

我做错了什么?

尝试删除 IActionResult 并将其设为 Void

public void SignOut()
{
    return SignOut(new AuthenticationProperties { RedirectUri = Url.Action(nameof(AfterSignOut)) }, AzureADDefaults.AuthenticationScheme);
}

public async Task SignOut() // Not sure if it has a signout async method but use this if it does
    {
        return await SignOutAsync(new AuthenticationProperties { RedirectUri = Url.Action(nameof(AfterSignOut)) }, AzureADDefaults.AuthenticationScheme);
    }

如果使用 Microsoft.AspNetCore.Authentication.AzureAD.UI 并使用身份验证,您可以尝试以下解决方案:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

方法一:

创建帐户控制器并编写您自己的注销操作:

public readonly IOptionsMonitor<AzureADOptions> Options;
public AccountController(IOptionsMonitor<AzureADOptions> options)
{
    Options = options;
}
public IActionResult SignOut()
{
    var options = Options.Get(AzureADDefaults.AuthenticationScheme);
    var callbackUrl = Url.Action(nameof(AfterSignOut), "Account", values: null, protocol: Request.Scheme);
    return SignOut(
        new AuthenticationProperties { RedirectUri = callbackUrl },
        options.CookieSchemeName,
        options.OpenIdConnectSchemeName);
}

方法二:

使用库中现有的注销功能,在 OnSignedOutCallbackRedirect 事件中设置新的重定向 url :

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
        .AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{

    options.Events.OnSignedOutCallbackRedirect = (context) =>
    {

        context.Response.Redirect("/Account/AfterSignOut");
        context.HandleResponse();

        return Task.CompletedTask;
    };
});

然后在要执行注销的页面中添加 link :

<a href="~/AzureAD/Account/SignOut">SignOut</a>

方法三:

使用自定义 URL Rewriting Middleware 通过检查路径重定向,在 app.UseMvc:

之前放置以下代码
app.UseRewriter(
    new RewriteOptions().Add(
        context => { if (context.HttpContext.Request.Path == "/AzureAD/Account/SignedOut")
            { context.HttpContext.Response.Redirect("/Account/AfterSignOut"); }
        })
);

还有 link : <a href="~/AzureAD/Account/SignOut">SignOut</a>