MSAL 未重定向到 Android 应用
MSAL not redirecting to Android app
我正在玩 MSAL(Microsoft Identity Client)预览版 1.1.2,但在登录过程中卡住了,它没有将我的应用程序重定向回 Android 应用程序。
我的 Xamarin 应用程序是 Prism 生成的 Android 应用程序,我相信我已经安排好了一切。
在App.xaml.cs
文件中,有:
public static UIParent UiParent { get; set; }
public static PublicClientApplication ClientApplication { get; set; }
public static string[] Scopes = { "User.Read" };
然后,在 App 构造函数中,我有:
public App(IPlatformInitializer initializer) : base(initializer)
{
ClientApplication = new PublicClientApplication("my-app-id");
}
请注意,这是 Prism 生成的应用程序。
然后,在我的 MainPageViewModel.cs
文件中,我有一个触发按钮点击的命令,代码如下:
private async void OnAuthenticate()
{
try
{
var result = await App.ClientApplication.AcquireTokenAsync(App.Scopes, App.UiParent);
Message = $"Welcome {result.User.Name}";
}
catch (MsalException ex)
{
Message = ex.Message;
}
}
为了正确解析 UIParent,我将 Droid 项目中的 MainActivity.cs 文件更新为:
protected override void OnCreate(Bundle bundle)
{
// omitted...
LoadApplication(new App(new AndroidInitializer()));
App.UiParent = new UIParent(this);
}
和:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
如果我取消登录过程,我会收到一条消息说 "User cancelled authentication",这是正确的,但登录过程永远不会将我重定向回 Android 应用程序。
我的应用程序中的重定向 URI 是这样的:
msal{my-app-id}://auth
根据我在 MSAL 和 Xamarin 上阅读的大多数博客,这是应该的。
不知道为什么它对我不起作用?
就像 JadedEric 提到的,更新您的 AndroidManifest.xml 文件 android 和 IOS 更新您的 info.plist 文件。请查看以下 post,因为它们准确解释了在这些文件中放置的内容:https://github.com/Azure-Samples/active-directory-xamarin-native-v2
这里还有另一件重要的事情是,您必须确保您在 azure 中注册的应用程序与 manfiest.xml 和 info.plist 中的应用程序具有相同的 RedirectURI。这是使重定向正常工作的关键(即您上面提到的 msal{my-app-id}://auth
)
建议您尚未使用 return auth url "msal{ 更新 AndroidManifest.xml 文件clientID}".
例如:
<data android:scheme="msalxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" android:host="auth" />
我最近使用 github 上提供的 sample 解决方案遇到了同样的问题。
上面的初始答案是一个选项,但是 Microsoft 的这个 link 用示例代码更好地解释了它。您可以更新 android 清单:
<activity android:name="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="msal{client_id}" android:host="auth" />
</intent-filter>
</activity>
或在代码中创建 activity:
[Activity]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
DataHost = "auth",
DataScheme = "msal{client_id}")]
public class MsalActivity : BrowserTabActivity
{
}
无论哪种方式,一旦客户端 ID 设置正确,它应该重定向回您的应用程序。
我正在玩 MSAL(Microsoft Identity Client)预览版 1.1.2,但在登录过程中卡住了,它没有将我的应用程序重定向回 Android 应用程序。
我的 Xamarin 应用程序是 Prism 生成的 Android 应用程序,我相信我已经安排好了一切。
在App.xaml.cs
文件中,有:
public static UIParent UiParent { get; set; }
public static PublicClientApplication ClientApplication { get; set; }
public static string[] Scopes = { "User.Read" };
然后,在 App 构造函数中,我有:
public App(IPlatformInitializer initializer) : base(initializer)
{
ClientApplication = new PublicClientApplication("my-app-id");
}
请注意,这是 Prism 生成的应用程序。
然后,在我的 MainPageViewModel.cs
文件中,我有一个触发按钮点击的命令,代码如下:
private async void OnAuthenticate()
{
try
{
var result = await App.ClientApplication.AcquireTokenAsync(App.Scopes, App.UiParent);
Message = $"Welcome {result.User.Name}";
}
catch (MsalException ex)
{
Message = ex.Message;
}
}
为了正确解析 UIParent,我将 Droid 项目中的 MainActivity.cs 文件更新为:
protected override void OnCreate(Bundle bundle)
{
// omitted...
LoadApplication(new App(new AndroidInitializer()));
App.UiParent = new UIParent(this);
}
和:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
如果我取消登录过程,我会收到一条消息说 "User cancelled authentication",这是正确的,但登录过程永远不会将我重定向回 Android 应用程序。
我的应用程序中的重定向 URI 是这样的:
msal{my-app-id}://auth
根据我在 MSAL 和 Xamarin 上阅读的大多数博客,这是应该的。
不知道为什么它对我不起作用?
就像 JadedEric 提到的,更新您的 AndroidManifest.xml 文件 android 和 IOS 更新您的 info.plist 文件。请查看以下 post,因为它们准确解释了在这些文件中放置的内容:https://github.com/Azure-Samples/active-directory-xamarin-native-v2
这里还有另一件重要的事情是,您必须确保您在 azure 中注册的应用程序与 manfiest.xml 和 info.plist 中的应用程序具有相同的 RedirectURI。这是使重定向正常工作的关键(即您上面提到的 msal{my-app-id}://auth
)
建议您尚未使用 return auth url "msal{ 更新 AndroidManifest.xml 文件clientID}".
例如:
<data android:scheme="msalxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" android:host="auth" />
我最近使用 github 上提供的 sample 解决方案遇到了同样的问题。
上面的初始答案是一个选项,但是 Microsoft 的这个 link 用示例代码更好地解释了它。您可以更新 android 清单:
<activity android:name="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="msal{client_id}" android:host="auth" />
</intent-filter>
</activity>
或在代码中创建 activity:
[Activity]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
DataHost = "auth",
DataScheme = "msal{client_id}")]
public class MsalActivity : BrowserTabActivity
{
}
无论哪种方式,一旦客户端 ID 设置正确,它应该重定向回您的应用程序。