【问题标题】:Auth redirect with React Native App (Azure B2C)使用 React Native App (Azure B2C) 进行身份验证重定向
【发布时间】:2022-11-11 13:44:09
【问题描述】:

我是 React Native 的新手,我正在尝试使用 Azure AD B2C 和 react-native-app-auth 授权我的应用程序。

当系统浏览器正在启动并且我可以登录时,我正在努力使用正确的应用程序方案重定向回应用程序。

Android 上大多数基于 Azure 的文档 (like hereherehere)指示我将签名和方案用作AndroidManifest.xml 中的活动,以便应用程序可以响应流程。

<activity
    android:name="com.microsoft.identity.client.BrowserTabActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="msauth"
            android:host="Enter_the_Package_Name"
            android:path="/Enter_the_Signature_Hash" />
    </intent-filter>
</activity>

但是react-native-app-auth(如herehere)的React Native 文档说我只需要在android/app/build.grandle 文件中设置appAuthRedirectScheme

我不确定哪种方法合适,理论上这应该不难。但是当我尝试任何一种方法时,我要么在成功登录后没有响应(即重定向可能发生但应用程序没有监听),要么应用程序完全挂起。

关于如何使重定向起作用有什么明确的方向吗?

更新

我通过react-native-app-auth 阅读文档

方案是 OAuth 重定向 URL 的开头,直到方案分隔符 (:) 字符。例如。如果您的重定向 uri 是 com.myapp://oauth,那么 url 方案将是 com.myapp。方案必须是小写的。

这似乎与方案是包名称的其他 Azure 文档的工作方式不同。就我而言,我不得不将方案更改为msauth

manifestPlaceholders = [appAuthRedirectScheme: 'msauth']

这可行,但这不会与其他应用程序冲突吗?最佳实践说我应该使包名称唯一,保持 RedirectUri 唯一。但是如果scheme设置为msauth,会不会和其他app冲突?

【问题讨论】:

    标签: android azure react-native redirect


    【解决方案1】:

    我遇到了同样的问题,将intent-filter 标签添加到AndroidManifest.xml 解决了它。

     <activity
       android:name=".MainActivity"
       android:label="@string/app_name"
       android:launchMode="singleTask">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
         <data android:scheme="msauth"
               android:host="<application's package name>"
               android:path="/<signature-hash>" />
       </intent-filter>
    </activity>
    

    build.gradle 只有包名。

    android {
        // ...
        defaultConfig {
            // ... 
    
            manifestPlaceholders = [
              appAuthRedirectScheme: "<application's package name>"
            ]
        }
        // ...
    }
    

    例子

    重定向 URI:msauth://com.myapp.foobar/Xfkopqweqweqfgjitr=

    AndroidManifest.xml

     <activity
       android:name=".MainActivity"
       android:label="@string/app_name"
       android:launchMode="singleTask">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
         <data android:scheme="msauth"
               android:host="com.myapp.foobar"
               android:path="/Xfkopqweqweqfgjitr=" />
       </intent-filter>
    </activity>
    

    申请build.gradle

    android {
        // ...
        defaultConfig {
            // ... 
    
            manifestPlaceholders = [
              appAuthRedirectScheme: "com.myapp.foobar"
            ]
        }
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      相关资源
      最近更新 更多