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

React Native Expo AuthSession集成Azure认证Android端失效问题

Expo AuthSession Azure认证Android端失效问题

按照Expo官方文档的Azure认证指南,我在React Native Expo项目中使用AuthSession实现Azure认证功能。该功能在iOS端运行正常,可自动生成AccessToken并关闭浏览器,但Android端完全失效,既无法生成AccessToken,浏览器也无法自动关闭。

运行状态截图

iOS端正常运行

  • iOS认证前页面
  • iOS Azure认证过程
  • iOS Azure认证成功并获取Token

Android端失效状态

  • Android认证前页面
  • Android Azure认证过程

认证代码片段

import { useState } from 'react';
import * as WebBrowser from 'expo-web-browser';
import {
  exchangeCodeAsync,
  makeRedirectUri,
  useAuthRequest,
  useAutoDiscovery,
} from 'expo-auth-session';
import { Button, Text, SafeAreaView, Platform } from 'react-native';

WebBrowser.maybeCompleteAuthSession();

export default function App() {
  // Endpoint
  const discovery = useAutoDiscovery(
    'https://login.microsoftonline.com/<TENANT_ID>/v2.0',
  );

  const redirectUri = makeRedirectUri({
        native: Platform.OS === 'ios' ? "msauth.com.myorg.myauthapp://auth" : "msauth://com.myorg.myauthapp/GTEWOFHNVBCDAPDICYVBSQTY%2B",
    });

  const clientId = '<CLIENT_ID>';

  // 存储JWT
  const [token, setToken] = useState<string | null>(null);

  // 请求配置
  const [request, , promptAsync] = useAuthRequest(
    {
      clientId,
      scopes: ['openid', 'profile', 'email', 'offline_access'],
      redirectUri,
    },
    discovery,
  );

  return (
    <SafeAreaView>
      <Button
        disabled={!request}
        title="Login"
        onPress={() => {
          promptAsync().then((codeResponse) => {
            if (request && codeResponse?.type === 'success' && discovery) {
              exchangeCodeAsync(
                {
                  clientId,
                  code: codeResponse.params.code,
                  extraParams: request.codeVerifier
                    ? { code_verifier: request.codeVerifier }
                    : undefined,
                  redirectUri,
                },
                discovery,
              ).then((res) => {
                setToken(res.accessToken);
              });
            }
          });
        }}
      />
      <Text>{token}</Text>
    </SafeAreaView>
  );
}

已尝试的解决方案

  • 使用expo-web-browser的WebBrowser.openAuthSessionAsync()实现,问题依旧
  • 参考相关帖子修改android/app/src/AndroidManifest.xml文件,未解决问题
  • 测试环境:Android的Expo开发构建设备

修改后的AndroidManifest.xml片段

<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:allowBackup="true" android:theme="@style/AppTheme" android:supportsRtl="true">
    <meta-data android:name="expo.modules.updates.ENABLED" android:value="false"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_CHECK_ON_LAUNCH" android:value="ALWAYS"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_LAUNCH_WAIT_MS" android:value="0"/>
    <activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen" android:exported="true" android:screenOrientation="portrait">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      <intent-filter android:label="auth">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
         <data android:scheme="myappscheme"   android:path="msauth://com.myorg.myauthapp/GTEWOFHNVBCDAPDICYVBSQTY%2B" 
         />
      </intent-filter>
    </activity>
  </application>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 22:53:19