asp.net-core - 在 .NET Core OpenID Connect 中间件中重定向?

标签 asp.net-core asp.net-core-mvc .net-core openid-connect asp.net-core-1.0

当我正在验证的 OpenID Connect 服务器返回一组特定的查询字符串时,我正在尝试处理一个场景。当条件匹配时,我想基本上将用户重定向到“拒绝访问”页面。无论出于何种原因,下面包含重定向的注释行从未真正触发。有没有更好/不同的方式来做我所追求的?

以下是 OpenID Connect 中间件在 中的配置方式Startup.cs :

services.Configure<OpenIdConnectOptions>(options => 
{

    // ...

    options.Events = new OpenIdConnectEvents
    {
        OnMessageReceived = context =>
        {
            if (context.HttpContext.Request.Query.ContainsKey("error"))
            {
                context.HandleResponse(); // <-- Fires
                context.Response.Redirect("/AccessDenied"); // <-- Redirect fires but user is not redirected
            }

            return Task.FromResult(0);
        }
    }
}

更新:通过以下调整使其工作:
options.Events = new OpenIdConnectEvents
{
    OnRemoteFailure = context =>
    {
        context.HandleResponse();
        context.Response.Redirect("AccessDenied?error=" + context.Failure.Message);

        return Task.FromResult(0);
    },
    // ...
};

最佳答案

你不应该在重定向之前调用 HandleResponse() ,因为在这种情况下你“告诉”停止处理 HTTP 管道中的请求。改成这样:

        if (context.HttpContext.Request.Query.ContainsKey("error"))
        {
            context.Response.Redirect("/AccessDenied"); 
            context.HandleResponse(); 
        }

关于asp.net-core - 在 .NET Core OpenID Connect 中间件中重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195244/

相关文章:

c# - ASP.NET Core 2 MVC 全局异常处理不起作用

c# - 如何在Miniprofiler for asp.net core 2.0中禁用加载静态文件的显示

asp.net-core - 移动特定 View /设备检测

asp.net-core - 从project.json读取版本和其他项目元数据

mysql - 每第二次命中获得 "Access denied for user..."

asp.net - 如何将 Active Directory 用于 ASP.Net 5 (MVC6) Intranet 应用程序

c# - 为什么在 .Net Core 中没有要覆盖的 Dispose 方法?

asp.net-core - 无法解密防伪 token

c# - DotNetCore 在 Windows 中捕获屏幕截图

c# - 如何使Azure服务总线客户端在发送消息时不参与环境事务