I'm playing around with MSAL (Microsoft Identity Client) preview 1.1.2, and am stuck during the sign in process, where it does not redirect my application back to the Android application.
My Xamarin application is a Prism-generated Android application, and I believe I have everything sorted.
In the App.xaml.cs file, have:
public static UIParent UiParent { get; set; }
public static PublicClientApplication ClientApplication { get; set; }
public static string[] Scopes = { "User.Read" };
Then, in the App constructor, I have:
public App(IPlatformInitializer initializer) : base(initializer)
{
ClientApplication = new PublicClientApplication("my-app-id");
}
Please take into account, this is a Prism-generated application.
Then, in my MainPageViewModel.cs file, I have a command trigger the button click, with the following code:
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;
}
}
To correctly parse the UIParent, I updated the MainActivity.cs file in the Droid project, to:
protected override void OnCreate(Bundle bundle)
{
// omitted...
LoadApplication(new App(new AndroidInitializer()));
App.UiParent = new UIParent(this);
}
and:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
If I cancel the sign-in process, I get a message back saying "User cancelled authentication", which is correct, but the sign-in process just never redirects me back to the Android application.
The Redirect URI in my application is this:
msal{my-app-id}://auth
which, according to most of the blogs I read on MSAL and Xamarin, is what it should be.
Don't know why it's not working for me?
I encountered the same problem recently with the sample solution provided on github.
The initial answer above is one option, but this link from Microsoft explains it better with example code. You can update the android manifest:
<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>
or create the activity in code:
[Activity]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
DataHost = "auth",
DataScheme = "msal{client_id}")]
public class MsalActivity : BrowserTabActivity
{
}
Either way, once the client ID is set correctly it should redirect back to your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With