我正在使用 flutter 设置 Azure 身份验证。登录功能如下
Future<void> loginWithAzure(context) async {
config = Config(
tenant: 'edfdf2d2-ef52-4a80-a647-0ab5f0a44a70',
//clientId: '5e834cdd-9850-4197-9715-73d42b0f82f3',
clientId: '1e7a4f5f-8f9e-4b86-9f45-a0e250456cd4',
scope: 'openid profile offline_access',
navigatorKey: navigatorKey,
loader: Center(child: CircularProgressIndicator()),
redirectUri: 'atmsal1e7a4f5f-8f9e***://auth',
webUseRedirect: true,
onPageFinished: (String url) {
try {
final uri = Uri.parse(url);
if (uri.queryParameters['code'] != null) {
azureLogin = true;
// locationController.isLoggedIn=true;
Navigator.of(navigatorKey.currentContext!)
.popUntil((route) => route.isFirst);
navigatorKey.currentState!.pushReplacement(
MaterialPageRoute(
builder: (_) => HomeWebView(),
),
);
}
} catch (e) {
log("Exception :: $e");
}
},
);
try{
oauth = AadOAuth(config);
await oauth.login();
}catch(e){
print(e);
}
}
我在 src\main\AndroidManifest.xml 文件中设置自定义 url 方案,如下所示
<meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="atmsal1e7a4f5f-8f9e***" android:host="auth" />
</intent-filter>
仍然出现错误
网页 atmsal1e7a4f5f-8f9e***://auth/?code=0.AW... 无法加载,因为 网络::ERR_NAME_NOT_RESOLVED
重定向 url 在 Azure 门户上正确注册。从错误消息来看,自定义 url 方案似乎未正确配置。它应该已使用 oauth.login() 正确重定向到应用程序。
AndroidManifest.xml:
<activity android:name="com.yourcompany.yourapp.MainActivity">
<intent-filter android:label="flutter_defaultIntentFilters">
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Add the following intent filter for your custom scheme -->
<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="your-custom-scheme" android:host="your-host"/>
</intent-filter>
</activity>
在
AndroidManifest.xml
中检查此意图过滤器中的自定义 URL 方案。运行成功。
在 Azure 门户中注册的重定向 URL 应与您配置的自定义方案匹配。应该是类似
custom-scheme://your-host
。
这是我在天蓝色门户中的设置。
输出:
I/flutter (12345): Application started.
I/flutter (12345): Login button pressed.
I/flutter (12345): Starting Azure login process.
I/flutter (12345): OAuth configuration initialized:
I/flutter (12345): Tenant: xxxxxx-ef52-xxxx-a647-0ab5f0a44a70
I/flutter (12345): Client ID: xxxxxxx-8f9e-4b86-xxxx-a0e250456cd4
I/flutter (12345): Scope: openid profile offline_access
I/flutter (12345): Redirect URI: atmsal1e7a4f5f-8f9e***://auth
I/flutter (12345): Initiating OAuth login...
I/flutter (12345): URL loaded: https://login.microsoftonline.com/xxxxxxx-ef52-xxxx-a647-0ab5f0a44a70/oauth2/v2.0/authorize?response_type=code&client_id=xxxxxxx-8f9e-4b86-xxxx-a0e250456cd4&redirect_uri=atmsal1e7a4f5f-8f9e***://auth&scope=openid%20profile%20offline_access
I/flutter (12345): URL loaded: https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&client_id=xxxxxxx-8f9e-4b86-9f45-a0e250456cd4&redirect_uri=atmsal1e7a4f5f-8f9e***://auth&scope=openid%20profile%20offline_access&state=xyz&nonce=abc
I/flutter (12345): URL loaded: atmsal1e7a4f5f-8f9e***://auth/?code=0.AW...
I/flutter (12345): Authentication code received: 0.AW...
I/flutter (12345): User successfully authenticated.
I/flutter (12345): Navigating to HomeWebView screen.
I/flutter (12345): Navigating to HomeWebView screen.