active-directory-xamarin-native-v2 icon indicating copy to clipboard operation
active-directory-xamarin-native-v2 copied to clipboard

Cannot get Android to complete login process

Open pete1854 opened this issue 3 years ago • 1 comments

Hi, I'm experiencing the same issues as in #108 even though I'm using the latest version of the sample (incl. MSAL 4.47.2).

I'm implementing the Broker scenario.

MainActivity.cs

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
    private const string AndroidRedirectURI = "msauth://com.simrishamn.sbar/hash-intentionally-removed"; // TODO - Replace with your redirectURI
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // configure platform specific params
        PlatformConfig.Instance.RedirectUri = AndroidRedirectURI;
        PlatformConfig.Instance.ParentWindow = this;
    }

    /// <summary>
    /// This is a callback to continue with the broker base authentication
    /// Info abour redirect URI: https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-client-application-configuration#redirect-uri
    /// </summary>
    /// <param name="requestCode">request code </param>
    /// <param name="resultCode">result code</param>
    /// <param name="data">intent of the actvity</param>
    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.simrishamn.sbar">
	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<queries>
		<package android:name="com.azure.authenticator" />
		<package android:name="UserDetailsClient.Droid" />
		<package android:name="com.microsoft.windowsintune.companyportal" />
		<!-- Required for API Level 30 to make sure we can detect browsers
        (that don't support custom tabs) -->
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<category android:name="android.intent.category.BROWSABLE" />
			<data android:scheme="https" />
		</intent>
		<!-- Required for API Level 30 to make sure we can detect browsers that support custom tabs -->
		<!-- https://developers.google.com/web/updates/2020/07/custom-tabs-android-11#detecting_browsers_that_support_custom_tabs -->
		<intent>
			<action android:name="android.support.customtabs.action.CustomTabsService" />
		</intent>
	</queries>
	<uses-sdk android:minSdkVersion="29" />
</manifest>

pete1854 avatar Oct 13 '22 10:10 pete1854

I've been having the exact same issue as well on Xamarin with the "Basic" scenario. I got our login to work for iOS but not for android. Both the sample project and the test project I created have the same issue as above where the system browser opens, everything works as expected until "Are you trying to sign in to {ApplicationName}?" and hitting Cancel and Continue does nothing at all. As a note I am using Chrome as the system browser. Changing it to Samsung Internet also results in the same. Nothing interesting shows up in the debugger except that

authResult = await builder.ExecuteAsync().ConfigureAwait(false);

never completes, so this doesn't seem like something we can control on our end.

EDIT:

I got it to work using embedded web view. For reasons I cannot explain it seems to bypass the problematic prompt and logs you straight in after authenticating.

if (Device.RuntimePlatform == Device.Android)
{
    builder.WithEmbeddedWebView(true);
}

That being said, I'm aware of system browsers being the best practice so perhaps this is still worth looking into.

derekdkim avatar Nov 09 '22 15:11 derekdkim