ASP.NET Core(Visual Studio 2022)授权重定向至当前URL而非Identity Server
授权流程未跳转至Identity Server的排查与解决
你的代码仅配置了Cookie认证方案,并将默认挑战Scheme设为oidc,但缺少OIDC认证的具体配置项,这是授权时无法重定向到Identity Server、反而跳转至当前应用URL的核心原因。
修复步骤
在现有认证配置后补充AddOpenIdConnect方法,完成OIDC方案的配置:
services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies", options => { options.Cookie.HttpOnly = true; // 你的其他Cookie配置项... }) .AddOpenIdConnect("oidc", options => { // 替换为你的Identity Server实际地址 options.Authority = "https://your-identity-server-domain"; // 与Identity Server中注册的客户端ID一致 options.ClientId = "your-registered-client-id"; // 机密客户端需配置此密钥,与Identity Server注册信息匹配 options.ClientSecret = "your-client-secret"; // 推荐使用code授权模式 options.ResponseType = "code"; // 开启令牌保存,以便后续请求使用 options.SaveTokens = true; // 添加需要请求的权限范围,根据实际需求调整 options.Scope.Add("openid"); options.Scope.Add("profile"); // 其他OIDC相关配置... });
额外检查项
- 验证
Authority地址可正常访问,且能获取到Identity Server的元数据(访问{Authority}/.well-known/openid-configuration确认返回有效JSON) - 确保Identity Server中注册的客户端重定向URI与当前应用的回调地址一致(默认回调地址为
https://your-app-domain/signin-oidc) - 确认
Program.cs中中间件顺序正确:app.UseAuthentication()必须在app.UseAuthorization()之前添加
内容的提问来源于stack exchange,提问作者Vladyslav Fomin
相关产品推荐
相关产品推荐

