ASP.NET应用登出调用preventSilentAccess致Chrome/Edge强制关闭求助
ASP.NET登出功能在Chrome/Edge中导致浏览器强制关闭的问题解决
问题重现
当前实现的登出JS代码在Firefox中正常运行,但在Chrome和Edge浏览器中,点击登出按钮后浏览器会被强制关闭(即使有多标签页也无警告),预期流程是先完成Chrome中的登出操作,再调用后端/logout接口。
当前代码:
$(document).on('click', '.button-logout', function () { dialog.confirm(translate('Warning'), translate('LoggedOutAccount'), async dialogResult => { debugger; if (dialogResult == true) { if (navigator.credentials && typeof PasswordCredential !== 'undefined') { loadPanel.indicatorShow(); try { await navigator?.credentials?.preventSilentAccess(); } catch (e) { debugger; } loadPanel.indicatorHide(); } location.replace('/logout'); } }); });
问题根源
Chrome和Edge对navigator.credentials.preventSilentAccess() API的处理逻辑与Firefox存在差异:该API用于阻止浏览器自动登录,在Chrome/Edge中调用后立即执行location.replace('/logout'),会导致浏览器的凭据处理流程与页面跳转流程冲突,进而触发浏览器进程异常终止。
解决方案
方案1:确保异步操作完全完成后再跳转
通过将登出逻辑封装为独立异步函数,显式确保preventSilentAccess的异步操作彻底完成后再执行页面跳转,避免时序冲突:
$(document).on('click', '.button-logout', function () { dialog.confirm(translate('Warning'), translate('LoggedOutAccount'), dialogResult => { debugger; if (dialogResult == true) { const performLogout = async () => { if (navigator.credentials && typeof PasswordCredential !== 'undefined') { loadPanel.indicatorShow(); try { await navigator?.credentials?.preventSilentAccess(); } catch (e) { debugger; } loadPanel.indicatorHide(); } location.href = '/logout'; }; performLogout(); } }); });
方案2:替换location.replace为location.href
location.replace会替换当前页面的历史记录条目,而location.href是添加新的历史记录,这种差异可避免与Chrome的凭据处理逻辑冲突:
// 替换原代码中的location.replace('/logout'); location.href = '/logout';
方案3:临时注释preventSilentAccess验证问题
如果不需要阻止自动登录功能,可先注释掉preventSilentAccess的调用,确认是否是该API导致的浏览器关闭问题:
// try { // await navigator?.credentials?.preventSilentAccess(); // } catch (e) { // debugger; // }
验证建议
- 先测试方案3,确认是否是
preventSilentAccess引发的问题; - 如果确实是该API导致,优先使用方案1调整异步时序;
- 若方案1无效,尝试方案2替换跳转方式。
内容的提问来源于stack exchange,提问作者graph User
相关产品推荐
相关产品推荐

