首页 > 解决方案 > Identity Server 4 隐式流 - 未知错误

问题描述

我有一个在 .NET 5 上运行的 Identity Server 4 以及一个使用 oidc-client.js 库的简单 JS SPA 客户端

问题是我无法让我的身份服务器使用隐式身份验证。我的 SPA 客户端在 https://localhost:44334 上运行,Identity Server 在 https://localhost:5005 上运行

在此处输入图像描述

当我单击登录时,我正在尝试使用 oidc-client.js 从 IdentityServer 获取令牌

  var config = {
        authority: "https://localhost:5005",
        client_id: "react-client",
        redirect_uri: "https://localhost:44334/callback.html",
        response_type: "id_token token",
        scope: "openid profile Api1",
        post_logout_redirect_uri: "https://localhost:44334/index.html",
    };
    var mgr = new Oidc.UserManager(config);


    mgr.getUser().then(function (user) {
        if (user) {
            log("User logged in", user.profile);
        }
        else {
            log("User not logged in");
        }
    });

    function login() {
        mgr.signinRedirect();
    }

这是我在身份服务器上的客户端配置

 return new IdentityServer4.Models.Client
                {
                    ClientId = "react-client",
                    ClientName = "React Client",
                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,

                    RedirectUris = { "https://localhost:44334/callback.html" },
                    PostLogoutRedirectUris = { "https://localhost:44334/index.html" },
                    AllowedCorsOrigins = { "https://localhost:44334" },

                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "Api1"
                    }
                };

现在的问题是,当我单击登录按钮时,浏览器正在重定向到 IdentityServer,但随后显示此错误。

在此处输入图像描述

我做错了什么?

标签: c#.netasp.net-coreoauthidentityserver4

解决方案


我发现了问题并解决了。问题是 Api1 未在 Identity Server 的 ApiScopes 配置中列出。IdentityServer 显示的错误非常笼统,无法解决问题。


推荐阅读