Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 7709541

Browse files
author
Tiago Brenck
authored
Merge pull request #111 from Azure-Samples/tibre/droidCodeActivity
Adding Activity in code
2 parents 0b66a42 + 511b2d9 commit 7709541

File tree

8 files changed

+59
-41
lines changed

8 files changed

+59
-41
lines changed

README.md

+29-24
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,40 @@ Your native application registration should include the following information:
8484
#### [OPTIONAL] Step 6a: Configure the iOS project with your app's return URI
8585
1. Open the `UserDetailsClient.iOS\info.plist` file in a text editor (opening it in Visual Studio won't work for this step as you need to edit the text)
8686
1. In the URL types, section, add an entry for the authorization schema used in your redirectUri.
87+
8788
```xml
8889
<array>
8990
<dict>
9091
<key>CFBundleURLName</key>
9192
<string>active-directory-b2c-xamarin-native</string>
9293
<key>CFBundleURLSchemes</key>
9394
<array>
94-
<string>msal[APPLICATIONID]</string>
95+
<string>msal[ClientID]</string>
9596
</array>
9697
<key>CFBundleTypeRole</key>
9798
<string>None</string>
9899
</dict>
99100
</array>
100101
```
101-
where `[APPLICATIONID]` is the identifier you copied in step 2. Save the file.
102+
103+
where `[ClientID]` is the identifier you copied in step 2. Save the file.
102104

103105
#### [OPTIONAL] Step 6b: Configure the Android project with your app's return URI
104106

105-
1. Open the `UserDetailsClient.Droid\Properties\AndroidManifest.xml`
106-
1. Add or modify the `<application>` element as in the following
107-
```xml
108-
<application>
109-
<activity android:name="microsoft.identity.client.BrowserTabActivity">
110-
<intent-filter>
111-
<action android:name="android.intent.action.VIEW" />
112-
<category android:name="android.intent.category.DEFAULT" />
113-
<category android:name="android.intent.category.BROWSABLE" />
114-
<data android:scheme="msal[APPLICATIONID]" android:host="auth" />
115-
</intent-filter>
116-
</activity>
117-
</application>
118-
```
119-
where `[APPLICATIONID]` is the identifier you copied in step 2. Save the file.
107+
1. Open the `UserDetailsClient.Droid\MsalActivity.cs` file.
108+
1. Replace `[ClientID]` with the identifier you copied in step 2.
109+
1. Save the file.
110+
111+
```csharp
112+
[Activity]
113+
[IntentFilter(new[] { Intent.ActionView },
114+
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
115+
DataHost = "auth",
116+
DataScheme = "msal[ClientID]")]
117+
public class MsalActivity : BrowserTabActivity
118+
{
119+
}
120+
```
120121

121122
### Step 7: Run the sample
122123

@@ -128,8 +129,8 @@ Your native application registration should include the following information:
128129

129130
#### Running in an Android Emulator
130131

131-
If you have issues with the Android emulator, please refer to [this document](https://github.com/Azure-Samples/active-directory-general-docs/blob/master/AndroidEmulator.md) for instructions on how to ensure that your emulator supports the features required by MSAL.
132-
132+
If you have issues with the Android emulator, please refer to [this document](https://github.com/Azure-Samples/active-directory-general-docs/blob/master/AndroidEmulator.md) for instructions on how to ensure that your emulator supports the features required by MSAL.
133+
133134
## About the code
134135

135136
The structure of the solution is straightforward. All the application logic and UX reside in UserDetailsClient (portable).
@@ -169,11 +170,13 @@ AuthenticationResult ar = await App.PCA.AcquireTokenInteractive(App.Scopes)
169170
.WithParentActivityOrWindow(App.ParentActivityOrWindow)
170171
.ExecuteAsync();
171172
```
172-
The `Scopes` parameter indicates the permissions the application needs to gain access to the data requested through subsequent web API call (in this sample, encapsulated in `OnCallApi`). Scopes should be input in the following format: `https://{tenant_name}.onmicrosoft.com/{app_name}/{scope_value}`
173+
174+
The `Scopes` parameter indicates the permissions the application needs to gain access to the data requested through subsequent web API call (in this sample, encapsulated in `OnCallApi`). Scopes should be input in the following format: `https://{tenant_name}.onmicrosoft.com/{app_name}/{scope_value}`
173175
174176
The `.WithParentActivityOrWindow()` is used in Android to tie the authentication flow to the current activity, and is ignored on all other platforms. For more platform specific considerations, please see below.
175177

176-
The sign out logic is very simple. In this sample we have just one user, however we are demonstrating a more generic sign out logic that you can apply if you have multiple concurrent users and you want to clear up the entire cache.
178+
The sign out logic is very simple. In this sample we have just one user, however we are demonstrating a more generic sign out logic that you can apply if you have multiple concurrent users and you want to clear up the entire cache.
179+
177180
```csharp
178181
var accounts = await App.GetAccountsAsync();
179182
foreach (var account in accounts.ToArray())
@@ -196,23 +199,26 @@ AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestC
196199
That line ensures that control goes back to MSAL once the interactive portion of the authentication flow ended.
197200

198201
In `OnCreate`, we need to add the following assignment:
202+
199203
```csharp
200204
App.ParentActivityOrWindow = this; // This activity
201205
```
202-
That code ensures that the authentication flows occur in the context of the current activity.
203206

207+
That code ensures that the authentication flows occur in the context of the current activity.
204208

205209
### iOS specific considerations
206210

207211
UserDetailsClient.iOS only requires one extra line, in AppDelegate.cs.
208-
You need to ensure that the OpenUrl handler looks as ine snippet below:
212+
You need to ensure that the OpenUrl handler looks as the snippet below:
213+
209214
```csharp
210215
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
211216
{
212217
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
213218
return true;
214219
}
215220
```
221+
216222
Once again, this logic is meant to ensure that once the interactive portion of the authentication flow is concluded, the flow goes back to MSAL.
217223

218224
In order to make the token cache work and have the `AcquireTokenSilentAsync` work multiple steps must be followed :
@@ -221,7 +227,6 @@ In order to make the token cache work and have the `AcquireTokenSilentAsync` wor
221227
1. In your project options, on iOS **Bundle Signing view**, select your `Entitlements.plist` file for the Custom Entitlements field.
222228
1. When signing a certificate, make sure XCode uses the same Apple Id.
223229

224-
225230
## More information
226231

227232
For more information on Azure B2C, see [the Azure AD B2C documentation homepage](http://aka.ms/aadb2c).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Android.App;
2+
using Android.Content;
3+
using Android.OS;
4+
using Android.Runtime;
5+
using Android.Views;
6+
using Android.Widget;
7+
using Microsoft.Identity.Client;
8+
9+
namespace UserDetailsClient.Droid
10+
{
11+
[Activity]
12+
[IntentFilter(new[] { Intent.ActionView },
13+
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
14+
DataHost = "auth",
15+
DataScheme = "msal90c0fe63-bcf2-44d5-8fb7-b8bbc0b29dc6")]
16+
public class MsalActivity : BrowserTabActivity
17+
{
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="UserDetailsClient.Droid">
33
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28" />
44
<uses-permission android:name="android.permission.INTERNET" />
55
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
66
<application>
7-
<activity android:name="microsoft.identity.client.BrowserTabActivity">
8-
<intent-filter>
9-
<action android:name="android.intent.action.VIEW" />
10-
<category android:name="android.intent.category.DEFAULT" />
11-
<category android:name="android.intent.category.BROWSABLE" />
12-
<data android:scheme="msal90c0fe63-bcf2-44d5-8fb7-b8bbc0b29dc6" android:host="auth" />
13-
</intent-filter>
14-
</activity>
7+
158
</application>
169
</manifest>

UserDetailsClient/UserDetailsClient.Droid/UserDetailsClient.Droid.csproj

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
1919
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
2020
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
21-
<AndroidSupportedAbis>arm64-v8a</AndroidSupportedAbis>
21+
<AndroidSupportedAbis>arm64-v8a;x86</AndroidSupportedAbis>
2222
<AndroidStoreUncompressedFileExtensions />
2323
<MandroidI18n />
2424
<JavaMaximumHeapSize />
@@ -50,8 +50,8 @@
5050
<ItemGroup>
5151
<Reference Include="Java.Interop" />
5252
<Reference Include="Microsoft.CSharp" />
53-
<Reference Include="Microsoft.Identity.Client, Version=4.6.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
54-
<HintPath>..\..\packages\Microsoft.Identity.Client.4.6.0\lib\monoandroid90\Microsoft.Identity.Client.dll</HintPath>
53+
<Reference Include="Microsoft.Identity.Client, Version=4.7.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
54+
<HintPath>..\..\packages\Microsoft.Identity.Client.4.7.1\lib\monoandroid90\Microsoft.Identity.Client.dll</HintPath>
5555
</Reference>
5656
<Reference Include="Mono.Android" />
5757
<Reference Include="mscorlib" />
@@ -201,6 +201,7 @@
201201
</ItemGroup>
202202
<ItemGroup>
203203
<Compile Include="MainActivity.cs" />
204+
<Compile Include="MsalActivity.cs" />
204205
<Compile Include="Resources\Resource.Designer.cs" />
205206
<Compile Include="Properties\AssemblyInfo.cs" />
206207
</ItemGroup>

UserDetailsClient/UserDetailsClient.Droid/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="AsyncUsageAnalyzers" version="1.0.0-alpha003" targetFramework="monoandroid81" developmentDependency="true" />
44
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="monoandroid81" />
5-
<package id="Microsoft.Identity.Client" version="4.6.0" targetFramework="monoandroid90" />
5+
<package id="Microsoft.Identity.Client" version="4.7.1" targetFramework="monoandroid90" />
66
<package id="Microsoft.NETCore.Platforms" version="2.1.0" targetFramework="monoandroid81" />
77
<package id="Microsoft.NETCore.Targets" version="1.1.3" targetFramework="monoandroid90" />
88
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="monoandroid60" />

UserDetailsClient/UserDetailsClient.UWP/UserDetailsClient.UWP.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
</ItemGroup>
133133
<ItemGroup>
134134
<PackageReference Include="Microsoft.Identity.Client">
135-
<Version>4.6.0</Version>
135+
<Version>4.7.1</Version>
136136
</PackageReference>
137137
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
138138
<Version>6.1.4</Version>

UserDetailsClient/UserDetailsClient.iOS/UserDetailsClient.iOS.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@
106106
</ItemGroup>
107107
<ItemGroup>
108108
<Reference Include="Microsoft.CSharp" />
109-
<Reference Include="Microsoft.Identity.Client, Version=4.6.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
110-
<HintPath>..\..\packages\Microsoft.Identity.Client.4.6.0\lib\xamarinios10\Microsoft.Identity.Client.dll</HintPath>
109+
<Reference Include="Microsoft.Identity.Client, Version=4.7.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
110+
<HintPath>..\..\packages\Microsoft.Identity.Client.4.7.1\lib\xamarinios10\Microsoft.Identity.Client.dll</HintPath>
111111
</Reference>
112112
<Reference Include="System" />
113113
<Reference Include="System.IdentityModel" />

UserDetailsClient/UserDetailsClient.iOS/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="AsyncUsageAnalyzers" version="1.0.0-alpha003" targetFramework="xamarinios10" developmentDependency="true" />
44
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="xamarinios10" />
5-
<package id="Microsoft.Identity.Client" version="4.6.0" targetFramework="xamarinios10" />
5+
<package id="Microsoft.Identity.Client" version="4.7.1" targetFramework="xamarinios10" />
66
<package id="Microsoft.NETCore.Platforms" version="1.1.1" targetFramework="xamarinios10" />
77
<package id="Microsoft.NETCore.Targets" version="1.1.3" targetFramework="xamarinios10" />
88
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="xamarinios10" />

0 commit comments

Comments
 (0)