Azure部署的ASP.NET站点Steam OpenID登录API仅清缓存后可调用求助
ASP.NET Azure部署Steam登录空白问题解决方案
1. 给登录接口禁用响应缓存
ASP.NET Core生产环境默认会启用响应缓存,本地开发时缓存默认关闭所以运行正常,部署到Azure后缓存生效导致302跳转响应被缓存,浏览器不再发起新请求。直接在SignIn接口上添加响应缓存禁用属性即可:
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] [HttpPost("api/signin"), HttpGet("api/signin")] public async Task<IActionResult> SignIn(string type) { string provider = type; if (string.IsNullOrWhiteSpace(provider)) { return BadRequest(); } if (!await HttpContext.IsProviderSupportedAsync(provider)) { return BadRequest(); } return Challenge(new AuthenticationProperties { RedirectUri = "api/saveuser" }, provider); }
2. 修正前端登录逻辑的时序错误
你当前的前端代码在打开登录窗口后立刻刷新父页面,会导致新窗口的请求还没发送就被浏览器中断,导致空白。需要把父页面刷新逻辑移动到登录窗口关闭的回调中:
handleLoginSteam = () => { let winHeight = window.screen.height / 1.5; let winWidth = window.screen.width / 3; const win = window.open( '/api/signin?type=Steam', 'Discord Sign In', "height=" + winHeight + ",width=" + winWidth + ",top=" + ((window.screen.height - winHeight) / 2) + ",left=" + ((window.screen.width - winWidth) / 2)); const timer = setInterval(() => { if (win.closed) { clearInterval(timer); // 登录完成窗口关闭后再刷新父页面、拉取用户数据 window.location.reload(); this.fetchUserData(); } }, 500); }
3. 配置Azure App Service全局API缓存规则
以上修改完代码后如果问题仍存在,可在Azure门户对应App Service的【HTTP响应头】配置页,添加全局添加响应头规则:
- 匹配路径:
/api/* - 响应头名称:
Cache-Control - 响应头值:
no-cache, no-store, must-revalidate
配置后重启App Service生效。
内容的提问来源于stack exchange,提问作者Cameron Fullilove
相关产品推荐
相关产品推荐

