【问题标题】:post_logout_redirect_uri ASP NET Core 2.2 AzureAD Razor Class Library RCLpost_logout_redirect_uri ASP NET Core 2.2 AzureAD Razor 类库 RCL
【发布时间】:2018-12-21 19:41:30
【问题描述】:

我们已经尝试使用示例 https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/ 浏览了样本和所有作品。 在注销过程后,我们无法让它重定向。此外,似乎帐户控制器不存在,但在 _layout.chtml 中调用了它,这一定是新的。

【问题讨论】:

  • 我测试了你提到的代码示例。它将用户重定向到页面显示“您要退出哪个帐户?” .选择一个帐户后,它将重定向回应用程序。您是修改代码示例还是手动将 Azure AD 身份验证添加到您的 .net core 2.2 应用程序?
  • 您也可以尝试在您的 azure 门户中添加注销 url (localhost:xxxx/signout-callback-oidc) 到允许的回复 url。
  • 是的,它确实重定向到应用程序 - 我希望它重定向到不同的页面。

标签: razor asp.net-core azure-active-directory openid-connect


【解决方案1】:

是的,它确实重定向到应用程序 - 我希望它重定向到不同的页面。

您可以通过设置OnSignedOutCallbackRedirect 事件在用户退出后将用户重定向到另一个页面:

  1. Startup.cs 添加using System.Threading.Tasks;
  2. OnSignedOutCallbackRedirect 事件中配置您的新重定向网址:

    services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
    {
        options.Authority = options.Authority + "/v2.0/";
    
        options.TokenValidationParameters.ValidateIssuer = false;
    
        options.Events.OnSignedOutCallbackRedirect = (context) =>
        {
    
            context.Response.Redirect("/Home/About");
            context.HandleResponse();
    
            return Task.CompletedTask;
        };
    });
    

【讨论】:

  • @twc,如果有帮助,您可以标记为答案,这可能会帮助遇到同样问题的其他人。
  • 这行得通,但我不得不删除 options.Authority 行
  • @NanYu,谢谢,这在 Core 3.0 上对我有用。我想更好地理解代码,但在OpenIdConnectHandler.cs 文件中找不到OnSignedOutCallbackRedirect 事件。我很好奇您究竟是如何知道如何以这种方式正确处理事件的?对于代码中发生的事情,我将不胜感激。
【解决方案2】:

帐户控制器代码现在已内置到框架中。您可以在 Microsoft.AspNetCore.Authentication.AzureAD.UI.AzureAD.Controllers.Internal 中看到它(参见https://github.com/aspnet/AADIntegration/blob/0efa96de73e3235fbfc55cfe51d9547a693010cc/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/Areas/AzureAD/Controllers/AccountController.cs):

namespace Microsoft.AspNetCore.Authentication.AzureAD.UI.AzureAD.Controllers.Internal
{
    [AllowAnonymous]
    [Area("AzureAD")]
    [NonController]
    [Route("[area]/[controller]/[action]")]
    internal class AccountController : Controller
    {
        public IOptionsMonitor<AzureADOptions> Options
        {
            get;
        }

        public AccountController(IOptionsMonitor<AzureADOptions> options)
        {
            this.Options = options;
        }

        [HttpGet("{scheme?}")]
        public IActionResult SignIn([FromRoute] string scheme)
        {
            scheme = scheme ?? AzureADDefaults.AuthenticationScheme;
            string str = base.Url.Content("~/");
            return this.Challenge(new AuthenticationProperties()
            {
                RedirectUri = str
            }, new String[] { scheme });
        }

        [HttpGet("{scheme?}")]
        public IActionResult SignOut([FromRoute] string scheme)
        {
            scheme = scheme ?? AzureADDefaults.AuthenticationScheme;
            AzureADOptions azureADOption = this.Options.Get(scheme);
            string str = base.Url.Page("/Account/SignedOut", null, null, base.Request.Scheme);
            return this.SignOut(new AuthenticationProperties()
            {
                RedirectUri = str
            }, new String[] { azureADOption.CookieSchemeName, azureADOption.OpenIdConnectSchemeName });
        }
    }
}

很遗憾,我无法在注销后强制重定向。相反,我看到一个页面显示“您已成功退出”。我想知道如何将用户重定向回索引页面。

【讨论】:

  • docs.microsoft.com/en-us/aspnet/core/razor-pages/… 当在 Web 应用程序和 Razor 类库中都找到视图、局部视图或 Razor 页面时,Web 应用程序中的 Razor 标记(.cshtml 文件)优先。我想我必须创建一个剃须刀页面来覆盖 RCL。
【解决方案3】:

我必须通过将其添加到控制器来手动覆盖已签名的页面:

    [AllowAnonymous]
    [HttpGet]
    [Route("/MicrosoftIdentity/Account/SignedOut")]
    public IActionResult SignedOut()
    {
        return Redirect(<MyRealSignedOutRedirectUri>);
    }

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2019-11-22
    • 2019-09-21
    • 2019-10-12
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2021-07-04
    相关资源
    最近更新 更多