登出后重定向在Asp.net Core 2中不起作用

问题描述 投票:0回答:2

我正在使用带有AzureAD身份验证的Asp.net Core 2.2。它可以正常工作,但是现在我很难尝试实现注销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仅返回HTTP代码200,仅此而已,它没有将我重定向到任何地方。

我在这里做错了什么?

asp.net-core asp.net-core-mvc azure-active-directory asp.net-core-2.2
2个回答
0
投票

尝试删除IActionResult并使其无效

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

OR

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);
    }

0
投票

如果使用Microsoft.AspNetCore.Authentication.AzureAD.UI并使用类似:的认证,则可以尝试以下解决方案:>

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

方法1:

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

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);
}

方法2:

使用库中的现有注销功能,在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;
    };
});

并且在您要执行登出的页面中添加链接:

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

方法3:

使用自定义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"); }
        })
);

也带有链接:<a href="~/AzureAD/Account/SignOut">SignOut</a>

© www.soinside.com 2019 - 2024. All rights reserved.