redirect - Identity Server 4 - 取消重定向到登录并返回 401

标签 redirect identityserver4 unauthorized

我有一个 ASP.NET Core 2 Web 应用没有身份服务器,我在其中配置了未经身份验证的 HTTP 请求的自动挑战,不将用户重定向到登录页面,而是只返回 401:

services.ConfigureApplicationCookie(config =>
        {
            config.Events.OnRedirectToLogin = ctx =>
            {
                ctx.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
                return Task.CompletedTask;
            };
        });

如何通过 Identity Server 4 集成实现同样的目标?我尝试在 AddOpenIdConnect 配置方法中使用相同的代码:

.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
    options.Events.OnRedirectToIdentityProvider = ctx =>
    {
        ctx.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
        return Task.CompletedTask;
    };
    [...]
})

虽然它不起作用,服务器还是返回 302...

最佳答案

我想为时已晚,但以下对我有用。如果有人有类似的问题。 (context.HandleResponse(); 在 OP 案例中很重要)

options.Events.OnRedirectToIdentityProvider = context =>
                 {
                     if (context.Request.Path.StartsWithSegments("/api"))
                     {
                         if (context.Response.StatusCode == (int)HttpStatusCode.OK)
                         {
                             context.ProtocolMessage.State = options.StateDataFormat.Protect(context.Properties);
                             context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                             context.Response.Headers["Location"] = context.ProtocolMessage.CreateAuthenticationRequestUrl();
                         }
                         context.HandleResponse();
                     }
                     return Task.CompletedTask;
                 };

关于redirect - Identity Server 4 - 取消重定向到登录并返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52812903/

相关文章:

redirect - Nginx 重定向 - 从 URL 中删除特定的查询参数

apache - 将带有参数的 URL 重定向到主页

php - 登录后重定向到上一页?

angular - Azure CDN - 与 Verizon 结合 Identity Server 4 重写 Angular 应用程序的 URL

javascript - 401(未经授权)在 Chrome 中,但在 IE 中没有

c# - 调用 Google GCM 时未经授权

python - 未经授权的客户端 : Grant type 'authorization_code' not allowed for the client. Django -auth0 -login

testing - 使用angularjs编写测试时如何处理重定向?

docker - 来自 caddy 后面的 duende 的 Identityserver 在 openid 配置中有 http

asp.net-core - IdentityServer AccessTokenValidation 结果中的 Token 未激活