根据 https://docs.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-android 的说明我使用 KeyTool 生成了开发签名哈希,并在 Azure 门户中注册了我的应用程序。
门户生成 MSAL 配置,然后我将其粘贴到项目中的 res.raw.auth_config.json 中(my-app-client-id 和 my-app 的包名称只是下面示例中的占位符;实际值由自动生成天蓝色):
{
"client_id" : "<my-app-client-id>",
"authorization_user_agent" : "DEFAULT",
"redirect_uri" : "msauth://<my-app's package name>/npYKnQBHywGAasZflTy2xmdpEiU%3D",
"authorities" : [
{
"type": "AAD",
"audience": {
"type": "AzureADMultipleOrgs",
"tenant_id": "organizations"
}
}
]
}
然后我将以下内容添加到 AndroidManifest.xml:
<application>
[..]
<!--Intent filter to catch Microsoft's callback after Sign In-->
<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"/>
<!--
Add in your scheme/host from registered redirect URI
note that the leading "/" is required for android:path
-->
<data
android:scheme="msauth"
android:host=""
android:path="/<signature-generated-by-keytool>" />
</intent-filter>
</activity>
[...]
</application>
在我的 Activity 的 onCreate() 中,我尝试创建一个单一帐户客户端应用程序,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_account_model);
initializeUI();
PublicClientApplication.createSingleAccountPublicClientApplication(this,
R.raw.auth_config,
new IPublicClientApplication.ISingleAccountApplicationCreatedListener() {
@Override
public void onCreated(ISingleAccountPublicClientApplication application) {
mSingleAccountApp = application;
loadAccount();
}
@Override
public void onError(MsalException exception) {
displayError(exception);
}
});
}
不幸的是 PublicClientApplication.createSingleAccountPublicClientApplication() 调用使应用程序崩溃并出现以下错误:
java.lang.IllegalStateException: The redirect URI in the configuration file doesn't match with the one generated with package name and signature hash. Please verify the uri in the config file and your app registration in Azure portal.
我无法弄清楚为什么会发生这种情况,因为配置文件的内容是由 Azure 门户生成的,并且包名称、签名哈希和重定向 URI 与门户中显示的任何内容相匹配。
我会很感激任何建议。
最佳答案
简而言之:这可能是您的调试 keystore 的问题。我最近遇到了同样的问题,当我使用他们的说明在命令行上创建哈希时,它最终成为了一个问题。为我解决的问题是在 azure 门户上重新执行应用注册过程,当您在命令行上创建哈希时,请确保使用您的 android keystore 的密码(默认为“android”)。我第一次这样做时,我使用了 jdk keystore 的默认密码,它仍然为我创建了一个哈希,但它导致了你描述的错误。我不完全确定整个过程是如何运作的,并且希望另一个人来澄清,但我上面描述的内容为我解决了这个问题。
关于android - 无法在 Android 上为 Azure AD 创建 SingleAccountPublicClientApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58757706/