ASP.NET MVC OIDC 令牌过期重定向到Home控制器及AADSTS50011错误解决
问题根因定位
你遇到的AADSTS50011错误,完全来自现有OIDC配置的逻辑问题:
- 原有代码在
RedirectToIdentityProvider通知中,会把请求跳转Azure AD时携带的RedirectUri赋值为用户当前正在访问的完整路径 - 当用户因为Cookie/Token过期访问
https://mywebsite.net/Home/Index时,发给Azure AD的认证请求会自动携带https://mywebsite.net/Home/Index作为目标回复地址 - 由于Azure AD应用注册中仅配置了
https://mywebsite.net/为合法回复地址,校验不通过就会抛出当前错误
这个问题完全不需要在Azure AD中为所有控制器、Action路由逐一追加回复URL,通过固定回复地址+状态传递的逻辑就能彻底解决
具体实现逻辑
所有调整仅需修改本地OIDC配置,零改动Azure AD应用注册的回复URL配置:
- 把OIDC流程中发给Azure AD的
RedirectUri固定为已经在Azure AD配置好的https://mywebsite.net/,不再随用户当前访问路径动态生成 - 借助OIDC协议自带的State参数,存储用户认证失效前的原始访问路径,避免用户登录完成后被强制落回站点根路径
- 统一在认证回调完成后解析State中存储的原始路径,引导用户回到目标地址;无有效路径时,默认落地到
Home/Index页面完成登录
调整后的OIDC配置参考代码
public void ConfigureAuth(IAppBuilder app) { FATContext db = new FATContext(); // 固定已在Azure AD配置的合法回复URL,全局复用 var validRedirectUri = new Uri("https://mywebsite.net/"); app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Cookies", CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(), }); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = clientId, Authority = Authority, // 固定登出跳转地址,与Azure AD配置匹配 PostLogoutRedirectUri = "https://mywebsite.net/Home/Index", TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, RoleClaimType = "groups", NameClaimType = "preferred_username", }, MetadataAddress = "MetaAddressHere", Notifications = new OpenIdConnectAuthenticationNotifications() { RedirectToIdentityProvider = (context) => { // 不再动态取当前路径当RedirectUri,固定为Azure AD已配置的合法地址 context.ProtocolMessage.RedirectUri = validRedirectUri.AbsoluteUri; // 仅在发起登录请求时传递原始访问路径,避免其他场景重复处理 if (context.ProtocolMessage.ResponseType == OpenIdConnectResponseType.CodeIdToken) { // 将用户原本要访问的路径存入OIDC State,登录完成后回跳 var originalPath = context.Request.PathBase + context.Request.Path + context.Request.QueryString; context.ProtocolMessage.State = Uri.EscapeDataString(originalPath); } return Task.FromResult(0); }, AuthorizationCodeReceived = (context) => { var code = context.Code; ClientCredential credential = new ClientCredential(clientId, appKey); string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID)); // 换token时使用和之前一致的固定RedirectUri,避免校验失败 return authContext.AcquireTokenByAuthorizationCodeAsync( code, validRedirectUri, credential, graphResourceId); }, // 认证完成后执行回跳逻辑 SecurityTokenValidated = (context) => { // 从State中解析用户原本要访问的路径 var state = context.ProtocolMessage.State; if (!string.IsNullOrEmpty(state)) { var originalPath = Uri.UnescapeDataString(state); // 防开放重定向校验,仅允许站内路径跳转 if (originalPath.StartsWith("/")) { context.Response.Redirect(originalPath); context.HandleResponse(); } } return Task.FromResult(0); } } }); }
实际运行效果
- 所有发送到Azure AD的认证请求,携带的回复地址始终固定为
https://mywebsite.net/,完全匹配现有的应用注册配置,不会再触发AADSTS50011匹配错误 - 无论用户在哪个路由地址触发Cookie/Token过期,都能正常走完登录流程,自动跳回之前要访问的页面;没有携带有效原路径时,默认落地
Home/Index - 全程不需要在Azure AD中新增任何单一路由的回复URL配置,无额外维护成本
内容的提问来源于stack exchange,提问作者Conor8630
相关产品推荐
相关产品推荐

