【问题标题】:Handle failed Silent Authentication in Open Id Connect在 Open Id Connect 中处理失败的静默身份验证
【发布时间】:2018-08-16 14:23:52
【问题描述】:

我有一个 ASP.NET 站点,它使用 Open Id Connect 向 Identity Server 进行身份验证。 当身份验证令牌即将到期时,我添加了一个静默身份验证(提示=无),它将更新令牌而不向用户显示任何登录对话框。 只要用户仍然登录到 Identity Server,这就可以正常工作。

如果用户不再登录,则返回“login_required”错误。我想通过让它静默失败并将用户重定向回身份验证开始的页面来处理此错误。 当向 AuthenticationFailed 通知返回错误时,RedirectUri 似乎不可用。报错后有什么办法可以访问RedirectUri?

我的配置看起来像这样(缩写):

    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions {
...
Notifications = new OpenIdConnectAuthenticationNotifications
{
    AuthenticationFailed = context =>
    {
        // if we failed to authenticate without prompt
        if (context.ProtocolMessage.Error == "login_required")
        {
            context.HandleResponse();
            //context.Response.Redirect("The url to the page where RedirectToIdentityProvider was triggered");
            return Task.FromResult(0);
        }

        context.HandleResponse();
        context.Response.Write(context.Exception.Message);
        return Task.FromResult(0);
    },
    RedirectToIdentityProvider = async context =>
    {
        ...

        if (ShouldReAuthenticate())
        {
            context.ProtocolMessage.SetParameter("prompt", "none");
        }

        ...
    }
}

});

【问题讨论】:

    标签: asp.net-mvc identityserver4 openid-connect


    【解决方案1】:

    我设法通过使用 ISecureDataFormat.Unprotect() 方法读取状态消息中的信息来解决这个问题。

    它可能可以做得更优雅,但是像这样:

            if (!string.IsNullOrEmpty(context.ProtocolMessage.State) ||
                context.ProtocolMessage.State.StartsWith("OpenIdConnect.AuthenticationProperties="))
            {
                var authenticationPropertiesString = context.ProtocolMessage.State.Split('=')[1];
    
                AuthenticationProperties authenticationProperties = context.Options.StateDataFormat.Unprotect(authenticationPropertiesString);
    
                return authenticationProperties.RedirectUri;
            }
    

    【讨论】:

    • 哇!我有完全相同的问题,它确实有效。所以谢谢你:) 我花了一段时间才找到这篇文章。
    • 很好的解决方案。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2023-03-16
    • 2020-10-16
    • 2018-12-10
    • 1970-01-01
    • 2011-09-09
    • 2019-06-26
    相关资源
    最近更新 更多