Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using activedirectoryxamarinintune;
using Android.Runtime;
using Android.Util;
using Microsoft.Intune.Mam.Client.Notification;
using Microsoft.Intune.Mam.Policy;
using Microsoft.Intune.Mam.Policy.Notification;
Expand All @@ -28,16 +30,34 @@ class EnrollmentNotificationReceiver : Java.Lang.Object, IMAMNotificationReceive
/// </returns>
public bool OnReceive(IMAMNotification notification)
{
Debug.WriteLine("***Begin EnrollmentNotificationReceiver -> OnReceive");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe log the notification type from the start.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the remapper needed? Is it only for CITI scenario? If so, since this is a public sample, can we put these changes into a dev app to not clutter the sample? Otherwise, I would clarify what the remapper does.
Similarly to the above, these log messages make it less clear on what's going on with the code.

if (notification.Type == MAMNotificationType.MamEnrollmentResult)
{
Debug.WriteLine("***Begin EnrollmentNotificationReceiver -> OnReceive (notification.Type == MAMNotificationType.MamEnrollmentResult)");
IMAMEnrollmentNotification enrollmentNotification = notification.JavaCast<IMAMEnrollmentNotification>();
MAMEnrollmentManagerResult result = enrollmentNotification.EnrollmentResult;

if (result.Equals(MAMEnrollmentManagerResult.EnrollmentSucceeded))
{
Debug.WriteLine($"***Begin OnReceive-> Enrollment = {result} Code = {result.Code}");
}
else if (notification.Type == MAMNotificationType.ComplianceStatus)
{
Debug.WriteLine("***Begin EnrollmentNotificationReceiver -> OnReceive (notification.Type == MAMNotificationType.ComplianceStatus)");
IMAMComplianceNotification complianceNotification = notification.JavaCast<IMAMComplianceNotification>();
MAMCAComplianceStatus result = complianceNotification.ComplianceStatus;
Debug.WriteLine($"***Begin OnReceive-> Compliance = {result} Code = {result.Code}");
if (result.Equals(MAMCAComplianceStatus.Compliant))
{
Log.Info(GetType().Name, "remediateCompliance succeeded; status = " + result);
Debug.WriteLine("***Begin OnReceive-> Compliant");
// this signals that MAM registration is complete and the app can proceed
PCAWrapper.MAMRegsiteredEvent.Set();
}
else
{
Log.Info(GetType().Name, "remediateCompliance failed; status = " + result);
Log.Info(GetType().Name, complianceNotification.ComplianceErrorTitle);
Log.Info(GetType().Name, complianceNotification.ComplianceErrorMessage);
}

}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using activedirectoryxamarinintune;
using Android.Util;
using Microsoft.Identity.Client;
using Microsoft.Intune.Mam.Client.App;
using Microsoft.Intune.Mam.Policy;

[assembly: Xamarin.Forms.Dependency(typeof(active_directory_xamarin_intune.Droid.IntuneMAMConnector))]
namespace active_directory_xamarin_intune.Droid
{
public class IntuneMAMConnector : IIntuneMAMConnector
Expand All @@ -19,26 +22,34 @@ public class IntuneMAMConnector : IIntuneMAMConnector
/// <returns></returns>
public async Task DoMAMRegisterAsync(IntuneAppProtectionPolicyRequiredException exProtection)
{
Log.Info(GetType().Name, "***Begin DoMAMRegisterAsync");
Debug.WriteLine("***Begin DoMAMRegisterAsync");
// reset the registered event
PCAWrapper.MAMRegsiteredEvent.Reset();

// Invoke compliance API on a different thread
await Task.Run(() =>
{
IMAMComplianceManager mgr = MAMComponents.Get<IMAMComplianceManager>();
mgr.RemediateCompliance(exProtection.Upn, exProtection.AccountUserId, exProtection.TenantId, exProtection.AuthorityUrl, false);
}).ConfigureAwait(false);
await Task.Run(() =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining why you do this on a background thread.

{
Log.Info(GetType().Name, "Attempting to remediateCompliance.");
Debug.WriteLine("***DoMAMRegisterAsync->RemediateCompliance");
IMAMComplianceManager mgr = MAMComponents.Get<IMAMComplianceManager>();
mgr.RemediateCompliance(exProtection.Upn, exProtection.AccountUserId, exProtection.TenantId, exProtection.AuthorityUrl, true);
}).ConfigureAwait(false);


// wait till the registration completes
// Note: This is a sample app for MSAL.NET. Scenarios such as what if enrollment fails or user chooses not to enroll will be as
// per the business requirements of the app and not considered in the sample app.
Debug.WriteLine("***DoMAMRegisterAsync->Pre-Wait");
Log.Info(GetType().Name, "***DoMAMRegisterAsync->Pre-Wait");
PCAWrapper.MAMRegsiteredEvent.WaitOne();
Debug.WriteLine("***DoMAMRegisterAsync->Post-Wait");
Log.Info(GetType().Name, "***DoMAMRegisterAsync->Post-Wait");
}

public void Unenroll()
{
// Not implpemented on Android.
{
// Not implpemented on Android by Broker. Known issue.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace active_directory_xamarin_intune.Droid
/// Due to an issue with debugging the Xamarin bound MAM SDK the Debuggable = false attribute must be added to the Application in order to enable debugging.
/// Without this attribute the application will crash when launched in Debug mode. Additional investigation is being performed to identify the root cause.
/// </remarks>
[Application(Debuggable = false)]
// [Application(Debuggable = false)]
#else
[Application]
#endif
Expand All @@ -33,8 +33,10 @@ public override void OnMAMCreate()

// Register the notification receivers to receive MAM notifications.
// Along with other, this will receive notification that the device has been enrolled.
var notificationRcvr = new EnrollmentNotificationReceiver();
IMAMNotificationReceiverRegistry registry = MAMComponents.Get<IMAMNotificationReceiverRegistry>();
registry.RegisterReceiver(new EnrollmentNotificationReceiver(), MAMNotificationType.MamEnrollmentResult);
registry.RegisterReceiver(notificationRcvr, MAMNotificationType.MamEnrollmentResult);
registry.RegisterReceiver(notificationRcvr, MAMNotificationType.ComplianceStatus);

base.OnMAMCreate();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using activedirectoryxamarinintune;
using Android.Content;
using Android.Widget;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.Widget;
using Android.Support.Design.Widget;
using Android.Views;

using Microsoft.Identity.Client;
using Microsoft.Intune.Mam.Client.App;
using Microsoft.Intune.Mam.Client.Support.V7.App;
using Microsoft.Intune.Mam.Policy;
using Android.Support.V4.View;
using activedirectoryxamarinintune;
using Android.Content.PM;

namespace active_directory_xamarin_intune.Droid
{
[Activity(Label = "Xamarin Intune Sample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
[Activity(Label = "Xamarin Intune Sample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
private const string AndroidRedirectURI = "msauth://com.yourcompany.xamarinintuneapp/JjmT52ASxa1Lz55s+qPPgxb5xeo="; // TODO - Replace with your redirectURI
private const string AndroidRedirectURI = "msauth://com.yourcompany.xamarinintuneapp/EHyvOdXj4uLXJXDaOMy5lwANmp0="; // TODO - Replace with your redirectURI

IIntuneMAMConnector _connector = new IntuneMAMConnector();

protected override void OnCreate(Bundle savedInstanceState)
protected override void OnMAMCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
base.OnMAMCreate(savedInstanceState);

Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

// configure platform specific params
PlatformConfigImpl.Instance.RedirectUri = AndroidRedirectURI;
PlatformConfigImpl.Instance.ParentWindow = this;

// register IntuneMAMConnector
Xamarin.Forms.DependencyService.RegisterSingleton<IIntuneMAMConnector>(_connector);
Xamarin.Forms.DependencyService.Register<IIntuneMAMConnector, IntuneMAMConnector>();

LoadApplication(new App());
}
Expand All @@ -49,9 +56,9 @@ public override void OnRequestPermissionsResult(int requestCode, string[] permis
/// <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)
protected override void OnMAMActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
base.OnMAMActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.yourcompany.XamarinIntuneApp">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:label="Xamarin Intune Sample" android:theme="@style/MainTheme"></application>
<application android:label="Xamarin Intune Sample" android:theme="@style/MainTheme">
<meta-data android:name="com.microsoft.intune.mam.aad.ClientID" android:value="236abc97-ec97-4804-a683-adcd8e23eeae" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Note: TODO - Replace with your TenantID -->
<meta-data android:name="com.microsoft.intune.mam.aad.Authority" android:value="https://login.microsoftonline.com/organizations" />
Expand Down
Loading