You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何关闭外部浏览器的Azure AD验证窗口或在应用内打开验证链接

Azure AD 交互式登录外部窗口问题解决方案

你可以通过以下两种方案解决问题:

方案1:使用应用内嵌WebView实现应用内登录(优先推荐)

该方案会强制登录流程在应用自带的内嵌WebView中完成,不会调用系统默认浏览器跳出当前应用,登录验证完成后内嵌窗口会自动关闭,体验连贯性最好。
修改代码如下:

IPublicClientApplication app = PublicClientApplicationBuilder
  .Create(azMapping.ClientId)
  .WithAuthority(authority)                                            
  .WithRedirectUri("http://localhost:44342/Default.aspx")
  .Build();

result = app.AcquireTokenInteractive(scopes)
  .WithUseEmbeddedWebView(true) // 新增此行,启用内嵌WebView
  .ExecuteAsync(CancellationToken.None)
  .GetAwaiter().GetResult();

方案2:保留系统默认浏览器打开,配置自动关闭窗口

如果需要复用系统浏览器的已登录Cookie状态,不想使用内嵌WebView,可以通过配置SystemWebViewOptions的返回页面注入自动关闭脚本,实现登录成功后自动关闭浏览器标签页。
修改代码如下:

IPublicClientApplication app = PublicClientApplicationBuilder
  .Create(azMapping.ClientId)
  .WithAuthority(authority)                                            
  .WithRedirectUri("http://localhost:44342/Default.aspx")
  .Build();

var webViewOptions = new SystemWebViewOptions()
{
    // 自定义登录成功后返回的页面,注入关闭窗口的JS脚本
    BrowserRedirectSuccessHtml = @"<html>
        <body>登录验证完成,窗口即将关闭...<script>setTimeout(()=>window.close(), 500);</script></body>
        </html>"
};

result = app.AcquireTokenInteractive(scopes)
  .WithSystemWebViewOptions(webViewOptions)
  .ExecuteAsync(CancellationToken.None)
  .GetAwaiter().GetResult();

内容的提问来源于stack exchange,提问作者Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 15:15:02