Identity Server 4:为什么我收到未经授权的客户端?

10

这是我连接身份服务器的mvc的初始设置。

 app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {                
            AuthenticationType = "oidc",
            SignInAsAuthenticationType = "Cookies",
            Authority = "http://identity.azurewebsites.net",
            RedirectUri = "http://localhost:62419/signin-oidc",
            PostLogoutRedirectUri = "http://localhost:62419/signout-callback-oidc",
            ClientId = "mvc", 
            ResponseType = "id_token",
            Scope = "openid profile",
            UseTokenLifetime = false,
            RequireHttpsMetadata = false,
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                SecurityTokenValidated = (context) =>
                {
                    var identity = context.AuthenticationTicket.Identity;
                    var name = identity.Claims.FirstOrDefault(c => c.Type == identity.NameClaimType)?.Value;

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

我可以访问身份验证服务器。我收到了一条消息

抱歉,出现错误:未经授权的客户端 无效的重定向URI

我已将redirectUri添加到与上面显示的代码匹配的ClientRedirectUris表中。是否还有其他区域我忘记添加或设置了。

请求网址:http://identity.azurewebsites.net/home/error?errorId=CfDJ8BPcf2qEDmRMt0TtYfAIujdUrTeIfqktT2TIcVFNomo6u6QFAROi-gEI2wXHP8kbmmiSYIK1aRV1nL-h6tFY_KeZabkMhIzy-V_0vvo2-hUFfj6I66qJWSjPiRhSYmGZa_-kYlULMb8a1Bz6UQ9UV5L6VdLscQRhScCpnOYpM6Ku84KM_S-4eZXrAX13EaVhqjxhpNhD8jIU9kJkjAn1t6sLVGrfZSEM0tAOGkTXFvBzuoucYURIFhZPJPGjVuJuRegrS2vsLPALHJCv3MLrW9ImudDeCkgf9VhAHwrRLfP3TB_7i4OvEffZwhuDuCSoyQ


请问您能否同时提供导致该错误的实际请求URL? - mackie
我已经添加了网址,那是您要找的吗? - LittleFunny
不是这个请求,而是你日志中应该先出现 /connect/authorize 端点的请求。 - mackie
我在错误的IdentityServer数据库中添加了RedirectUri。现在我可能没有无效的redirect_uri错误,但是ExternalLoginCallback函数中的loginInfo为空,并且返回到IdentityServer的登录界面。 - LittleFunny
对我来说,我需要更新Identity Server数据库中的ClientRedirectUris表。 - heringer
2个回答

7

你需要确保重定向URL与IdentityServer中的客户端配置中的重定向URL匹配。例如:

    new Client
    {
        ClientId = "mvc",
        ClientName = "MVC Client",
        AllowedGrantTypes = GrantTypes.Implicit,

        // where to redirect to after login
        RedirectUris = { "http://localhost:62419/signin-oidc" },

        // where to redirect to after logout
        PostLogoutRedirectUris = { "http://localhost:62419/signout-callback-oidc" },

        AllowedScopes = new List<string>
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile
        }
    }

请确保RedirectUris与客户端设置的重定向url 'http://localhost:62419/signin-oidc' 匹配。


在我的情况下,重定向URL中的http/https差异导致了该错误。谢谢! - opewix

3

另外,请确保您的作用域与客户端配置中的 AlowedScopes 匹配。如果我们能够看到请求 URL,那将非常有帮助,例如:

https://identity.azurewebsites.net/connect/authorize?
client_id=mvc
&redirect_uri=http://localhost:62419/signin-oidc
&response_type=id_token
&scope=openid profile
&nonce=63653346343504
&state=CfDJAJDR
&response_mode=form_post

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接