【问题标题】:Trying to change OIDC OnRedirectToIdentityProvider from 302 to 401尝试将 OIDC OnRedirectToIdentityProvider 从 302 更改为 401
【发布时间】:2020-03-06 20:56:25
【问题描述】:

我们正在使用带有自定义登录页面的 Identity Server 4。我们一直在测试 cookie 过期或手动修改令牌(强化的一部分)时会发生什么。

应用程序想要将我们重定向到https://localhost:44349/signin-oidc,并返回一个 302 重定向,但 /signin-oidc 不存在。我们想抛出 401 并将它们重定向到 /home。

我找到了可以捕捉事件的地方

options.Events = new OpenIdConnectEvents
    {
      OnRedirectToIdentityProvider = context => {
      var newRedirect =  context.ProtocolMessage.RedirectUri.ToString().Replace("signin-oidc", "home");
      var builder = new UriBuilder(newRedirect);
      builder.Scheme = "https";
      context.ProtocolMessage.RedirectUri = builder.ToString();
      context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
      return Task.FromResult(0);
     }
    };

但这似乎不起作用,我们只是抛出 302 错误,而没有任何方式向用户报告发生了什么。

【问题讨论】:

    标签: openid-connect


    【解决方案1】:

    找到这个小宝石here

    OnRedirectToIdentityProvider = ctx =>
    {
        if (ctx.Request.Path.StartsWithSegments("/api"))
        {
            if (ctx.Response.StatusCode == (int)HttpStatusCode.OK)
            {
                ctx.Response.StatusCode = 401;
            }
    
            ctx.HandleResponse();
        }
    
        return Task.CompletedTask;
    }
    

    这正是我需要的,即返回 401。诀窍在于 HandleResponse()。

    【讨论】:

    • 如何获取重定向位置?
    • 简单地说,哇...我在到达这个之前尝试了很多解决方案(失败)。谢谢
    • 见鬼,HandleResponse 似乎不再适用于 Core 3.0
    • 我在 3.1 上关注了这个,HandleResponse 工作了。谢谢。
    • 我需要位置,这里不存在。
    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2022-10-08
    • 2017-07-07
    • 2023-03-16
    相关资源
    最近更新 更多