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

Flutter的oauth_webauth包能否检测导航请求URL以打开Authenticator?

关于oauth_webauth包拦截导航请求URL的方案
  • oauth_webauth包本身支持拦截导航请求,你可以通过OAuthWebAuth.authorize方法中的navigationDelegate参数,实现类似webview_flutter的URL检测与拦截逻辑。
  • 具体实现步骤:
    1. 调用OAuthWebAuth.authorize时,传入自定义的NavigationDelegate,重写onNavigationRequest方法。
    2. 在onNavigationRequest中判断请求URL是否为Microsoft Authenticator的自定义scheme(通常以msauth://开头)。
    3. 若是自定义scheme,用url_launcher包唤起原生应用,同时返回NavigationDecision.prevent阻止WebView加载该URL,避免触发ERR_UNKNOWN_URL_SCHEME错误。

示例代码:

import 'package:oauth_webauth/oauth_webauth.dart';
import 'package:url_launcher/url_launcher.dart';

final authResult = await OAuthWebAuth.authorize(
  url: '你的Azure AD B2C授权URL',
  redirectUrl: '你的回调URL',
  clientId: '你的客户端ID',
  navigationDelegate: (NavigationRequest request) async {
    // 检测Authenticator的自定义scheme
    if (request.url.startsWith('msauth://')) {
      if (await canLaunchUrl(Uri.parse(request.url))) {
        await launchUrl(Uri.parse(request.url));
      }
      // 阻止WebView加载该URL
      return NavigationDecision.prevent;
    }
    // 其他URL允许正常加载
    return NavigationDecision.navigate;
  },
);
  • 额外配置要求:
    • Android:在AndroidManifest.xml中添加对应scheme的<intent-filter>,允许应用处理msauth://链接。
    • iOS:在Info.plist中添加LSApplicationQueriesSchemes数组并包含msauth,同时配置对应的CFBundleURLSchemes。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 01:37:12