You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Identity Server 6与WinForms客户端集成时出现Invalid redirect_uri错误

.NET 6 WinForms + IdentityServer6 重定向URI无效问题排查与解决

问题描述

在.NET 6 WinForms应用中基于IdentityServer6开发交互式登录表单,参考WinFormsWebView2的OIDC客户端示例实现时,遇到以下错误:

  • 客户端WebView提示:Sorry, there was an error : invalid_request Invalid redirect_uri
  • IdentityServer6控制台日志显示:Invalid redirect_uri: http://localhost/winforms.client,且日志中AllowedRedirectUris为空数组

已在IdentityServer客户端配置中添加该重定向URI,客户端代码也配置了相同的RedirectUri,但仍报错。环境版本:客户端使用IdentityModel.OidcClient v5.2.1,服务端使用IdentityServer v6.3.2。

相关代码

IdentityServer客户端配置

public static IEnumerable<Client> Clients =>
        new List<Client>
        {
            new Client
            {
                ClientId = "vr2-win-client",
             
                AllowedScopes = new List<string>
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile
                },

                RequireClientSecret = false,
                AllowedGrantTypes = GrantTypes.Code,
                RequirePkce = true,
                RedirectUris = { "https://notused", "http://localhost/winforms.client" },
                PostLogoutRedirectUris = { "https://notused" },
                AllowOfflineAccess = true,
                RequireConsent = false

                // scopes that client has access to
                //AllowedScopes = { "vr2-client-api" }
            }
        };

客户端登录代码

private async void LoginBtn_Click(object sender, EventArgs e)
{
    var oidcOptions = new OidcClientOptions
    {
        Authority = config["IdentityServerAddress"].ToString(),
        ClientId = "vr2-win-client",
        Scope = "vr2-client-api offline_access openid",
        RedirectUri = "http://localhost/winforms.client",
        Browser = this.loginWebForm
    };
    
    OidcClient _oidcClient = new OidcClient(oidcOptions);
                
    LoginResult loginResult = await _oidcClient.LoginAsync();

    if (loginResult.IsError)
    {
        MessageBox.Show(loginResult.Error, "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    this.accessTokenLifetimeManager = new AccessTokenLifetimeManager
        (loginResult, 600, cancellationOnExitTokenSource.Token, config);

    var refreshTokenTask = Task.Run(accessTokenLifetimeManager.PeriodicallyRefreshAccessTokensWorkerTaskAsync);

    vr2Button.Enabled = true;
} 

排查思路与解决建议

1. 确认客户端配置是否被正确加载

日志中AllowedRedirectUris为空,核心原因是IdentityServer未读取到目标客户端的配置。需检查:

  • 确保在IdentityServer服务注册时,已将客户端配置集合注入:例如在Program.cs中调用builder.Services.AddIdentityServer().AddInMemoryClients(Clients)
  • 排查是否存在多个客户端配置集合,是否出现配置覆盖的情况
  • 重启IdentityServer服务,确保配置修改已生效

2. 严格保证重定向URI的一致性

  • 客户端代码中的RedirectUri必须与IdentityServer配置中的RedirectUris完全匹配,包括大小写、斜杠、端口(如有)。若OIDC客户端自动添加尾部斜杠,可在服务端配置中同时添加http://localhost/winforms.client/作为兼容项
  • 捕获认证请求(如用Fiddler),确认请求参数中的redirect_uri与配置完全一致

3. 修正客户端权限范围配置

客户端请求的Scope包含vr2-client-api,但服务端配置中该客户端的AllowedScopes仅包含openid和profile,需将vr2-client-api添加到服务端的AllowedScopes列表中,否则会引发额外的权限错误

4. 强化日志与调试

  • 开启IdentityServer的详细日志(在appsettings.json中设置Logging.LogLevel.IdentityServer4为Debug),查看请求中的client_id是否正确,确认目标客户端是否被正确识别
  • 在客户端代码中输出oidcOptions的完整配置,排查是否存在代码逻辑修改了RedirectUri或ClientId的情况

内容的提问来源于stack exchange,提问作者user3561406

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:27:40