React Native Expo AuthSession集成Azure认证Android端失效问题
Expo AuthSession Azure认证Android端失效问题
按照Expo官方文档的Azure认证指南,我在React Native Expo项目中使用AuthSession实现Azure认证功能。该功能在iOS端运行正常,可自动生成AccessToken并关闭浏览器,但Android端完全失效,既无法生成AccessToken,浏览器也无法自动关闭。
运行状态截图
iOS端正常运行
Android端失效状态
认证代码片段
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
相关产品推荐
相关产品推荐






