【问题标题】:Redirect after signout not working in Asp.net Core 2注销后重定向在 Asp.net Core 2 中不起作用
【发布时间】:2020-02-09 01:05:47
【问题描述】:

我正在使用带有 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 网址,如 AuthenticationProperties 中指定的那样。相反,/SignOut 只是返回 HTTP 代码 200,仅此而已,它不会将我重定向到任何地方。

我在这里做错了什么?

【问题讨论】:

  • 您是否尝试在您的OpenIdConnectOptions 配置上设置:options.SignedOutRedirectUri = "your-after-sign-out-url"
  • 是的,我也试过了,但没有任何反应

标签: c# asp.net-core azure-active-directory asp.net-core-mvc asp.net-core-2.2


【解决方案1】:

如果使用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 事件中设置新的重定向网址:

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>

方法三:

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

还有链接:&lt;a href="~/AzureAD/Account/SignOut"&gt;SignOut&lt;/a&gt;

【讨论】:

    【解决方案2】:

    尝试删除 IActionResult 并使其无效

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-28
      • 2023-04-08
      • 2022-10-19
      • 2012-07-20
      • 2023-03-10
      • 2016-08-23
      • 2013-09-15
      • 2016-03-11
      相关资源
      最近更新 更多