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

Expo Auth Session集成Microsoft Entra ID:移动端登出异常

Expo集成Microsoft Entra ID登出重定向失败问题

登录流程正常,但登出功能存在问题——登出后的重定向完全无法触发。用户在弹窗中完成Microsoft的登出操作后,无法自动重定向回应用,只能手动关闭弹窗,此时返回的result类型为dismiss,和未完成登出就关闭窗口的结果一致。目前不得不临时注释结果检查逻辑,导致用户未选择登出账户时也会清除认证状态。

已完成的基础配置

  • 在app.json中设置scheme为myapp
  • 在Microsoft Entra ID中注册myapp://redirect作为Native/Desktop应用的重定向URL
  • 按照Expo文档指引,使用Expo Auth Session搭配Expo Web Browser实现认证

核心代码示例

认证服务配置

const autoDiscovery = AuthSession.useAutoDiscovery(
  `https://login.microsoftonline.com/${process.env.EXPO_PUBLIC_TENANT_ID}/v2.0`
);

const redirectUri = AuthSession.makeRedirectUri({
  path: 'redirect',
});

const [request, , promptAsync] = AuthSession.useAuthRequest(
  {
    clientId: String(process.env.EXPO_PUBLIC_CLIENT_ID),
    scopes: ['openid', 'profile', 'email', 'offline_access', 'User.Read'],
    redirectUri,
  },
  autoDiscovery
);

登出方法

async function signOut() {
  const result = await WebBrowser.openAuthSessionAsync(
    `${
      autoDiscovery?.endSessionEndpoint
    }?post_logout_redirect_uri=${encodeURIComponent(redirectUri)}`,
    redirectUri
  );

  // if (result.type === 'success') {
  await clearAuthState();
  setAuth(null);
  // }
}

已尝试的无效方案

  • 通过Expo Linking监听重定向事件,试图手动清除认证状态
  • 调整post_logout_redirect_uri参数,添加斜杠
  • 查阅相关旧帖并按指引操作,问题仍未解决

项目依赖版本

"dependencies": {
  "@date-fns/utc": "^1.2.0",
  "@expo/react-native-action-sheet": "^4.1.0",
  "@expo/vector-icons": "^14.0.3",
  "@hookform/resolvers": "^3.9.0",
  "@react-native-community/netinfo": "11.3.1",
  "@react-navigation/drawer": "^6.7.2",
  "@react-navigation/native": "^6.0.2",
  "@shopify/react-native-skia": "1.2.3",
  "@tanstack/query-async-storage-persister": "^5.53.1",
  "@tanstack/react-query": "^5.51.11",
  "@tanstack/react-query-persist-client": "^5.53.1",
  "axios": "^1.7.3",
  "class-variance-authority": "^0.7.0",
  "date-fns": "^3.6.0",
  "eventemitter3": "^5.0.1",
  "expo": "^51.0.34",
  "expo-auth-session": "~5.5.2",
  "expo-background-fetch": "~12.0.1",
  "expo-blur": "~13.0.2",
  "expo-constants": "~16.0.2",
  "expo-crypto": "~13.0.2",
  "expo-dev-client": "~4.0.27",
  "expo-font": "~12.0.10",
  "expo-image": "~1.13.0",
  "expo-linking": "~6.3.1",
  "expo-router": "~3.5.23",
  "expo-secure-store": "~13.0.2",
  "expo-splash-screen": "~0.27.6",
  "expo-status-bar": "~1.12.1",
  "expo-system-ui": "~3.0.7",
  "expo-task-manager": "~11.8.2",
  "expo-web-browser": "~13.0.3",
  "nativewind": "^2.0.11",
  "react": "18.2.0",
  "react-dom": "18.2.0",
  "react-hook-form": "^7.52.2",
  "react-native": "0.74.5",
  "react-native-currency-input": "^1.1.1",
  "react-native-gesture-handler": "~2.16.2",
  "react-native-get-random-values": "~1.11.0",
  "react-native-keyboard-aware-scroll-view": "^0.9.5",
  "react-native-masked-text": "^1.13.0",
  "react-native-reanimated": "~3.10.1",
  "react-native-reanimated-carousel": "4.0.0-alpha.12",
  "react-native-safe-area-context": "4.10.5",
  "react-native-screens": "3.31.1",
  "react-native-svg": "15.2.0",
  "react-native-ui-datepicker": "^2.0.3",
  "react-native-web": "~0.19.10",
  "realm": "^12.13.1",
  "tailwind-merge": "^2.4.0",
  "uuid": "^10.0.0",
  "zod": "^3.23.8",
  "zustand": "^4.5.4"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 15:22:37