gpt4 book ai didi

c# - MSAL 未重定向到 Android 应用

转载 作者:行者123 更新时间:2023-12-05 00:00:09 24 4
gpt4 key购买 nike

我正在使用 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);
}

如果我取消登录过程,我会收到一条消息说“用户已取消身份验证”,这是正确的,但登录过程永远不会将我重定向回 Android 应用程序。

我的应用程序中的重定向 URI 是这样的:

msal{my-app-id}://auth

根据我在 MSAL 和 Xamarin 上阅读的大多数博客,它应该是这样的。

不知道为什么它对我不起作用?

最佳答案

我最近遇到了同样的问题 sample github上提供的解决方案。

上面的初始答案是一个选项,但是这个link来自微软的示例代码更好地解释了它。您可以更新 android list :

<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 设置正确,它应该重定向回您的应用程序。

关于c# - MSAL 未重定向到 Android 应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49500311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com