首页 > 解决方案 > 使用 IdentityServer4 时如何在客户端检查 access_denied?

问题描述

我有一个登录页面,当用户单击取消按钮时我需要将他重定向到访问客户端应用程序上的拒绝页面。

内部登录操作:

 if (button != "login")
        {
            // the user clicked the "cancel" button
            var context = await interaction.GetAuthorizationContextAsync(model.ReturnUrl);
            if (context != null)
            {
                // if the user cancels, send a result back into IdentityServer as if they 
                // denied the consent (even if this client does not require consent).
                // this will send back an access denied OIDC error response to the client.
                await interaction.GrantConsentAsync(context, ConsentResponse.Denied);

                // we can trust model.ReturnUrl since GetAuthorizationContextAsync returned non-null
                return Redirect(model.ReturnUrl);
            }
        }

在客户端(MVC)上,我配置了以下事件:

options.Events = new OpenIdConnectEvents
                {
                    OnRemoteFailure = context => 
                    {
                        // here it's returned as 200 ok in case I denied 
                        // consent should'nt be 401 access denined??
                        var statusCode=context.Response.StatusCode;
                        context.Response.Redirect("/");
                        context.HandleResponse();

                        return Task.FromResult(0);
                    }
                };

但我的问题是:我怎么知道 IdentityServer4 失败是因为用户单击了取消按钮(access_denied),还是有其他问题导致了失败?

标签: c#identityserver4openid-connect

解决方案


在 IdentityServer 端:

基本形式有 2 个按钮:logincancel。如果login没有按下;这是一个cancel

否则它是一个验证错误,你可以显示它。在cancel你应该重定向回一个有意义的页面。

在 MVC 方面:

您可以使用额外的参数进行重定向。这些可以被获取并用于显示错误。请记住,许多错误处理,例如无效的用户名/密码都留在 IdentityServer 端。


推荐阅读