移除OpenIdConnectAuthenticationOptions的CallbackPath引发IDX21323错误及跳转问题求助
问题原因
你之前通过修改CallbackPath = new PathString("/MyWebFileDirectory/signin-azuread-oidc")解决IDX21323错误的操作是正确的,后续跳转首页是因为OpenID Connect中间件默认在自定义CallbackPath的场景下,不会自动保留用户触发认证前访问的原始目标路径,认证成功后默认回退到网站根路径(首页)。
解决方案
按以下步骤修改认证配置即可:
- 找到你的身份验证配置代码位置:.NET Core/5+版本在
Program.cs的认证服务注册段,.NET Framework版本在App_Start/Startup.Auth.cs的ConfigureAuth方法内 - 给
OpenIdConnectOptions添加事件配置,保留原始访问路径用于认证后重定向,代码示例如下:
// .NET Core/5+ 版本示例 services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(options => { // 保留你已有的CallbackPath配置 options.CallbackPath = new PathString("/MyWebFileDirectory/signin-azuread-oidc"); // 新增事件配置 options.Events ??= new OpenIdConnectEvents(); // 跳转到Azure AD认证前,记录用户原本要访问的地址 options.Events.OnRedirectToIdentityProvider = context => { if (context.Request.Query.TryGetValue("ReturnUrl", out var targetUrl)) { context.Properties.RedirectUri = targetUrl; } return Task.CompletedTask; }; });
// .NET Framework MVC 版本示例 app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { // 保留你已有的ClientId、Authority、CallbackPath等配置 CallbackPath = new PathString("/MyWebFileDirectory/signin-azuread-oidc"), // 新增事件配置 Notifications = new OpenIdConnectAuthenticationNotifications { RedirectToIdentityProvider = context => { if (context.Request.Query.TryGetValue("ReturnUrl", out var targetUrl)) { context.ProtocolMessage.RedirectUri = targetUrl; } return Task.FromResult(0); } } });
- 检查Azure门户中应用注册的重定向URI配置,确保完整回调地址(如
https://你的域名/MyWebFileDirectory/signin-azuread-oidc)已添加到「身份验证」-「Web」分类的重定向URI列表中,没有拼写、大小写、路径错误 - 确认IIS部署的虚拟目录名称和
CallbackPath前缀/MyWebFileDirectory完全一致,没有多余路径层级
验证方法
直接在浏览器地址栏输入带
[Authorize]属性的Action的完整访问路径,触发Azure AD认证,完成后系统会直接跳转到你输入的目标路径,不会再跳转首页。
内容的提问来源于stack exchange,提问作者Maggy3
相关产品推荐
相关产品推荐

