Skip to content

Commit

Permalink
Improving the Readme.md and the configuration script based on suggest…
Browse files Browse the repository at this point in the history
…ions by Kalyan in another sample.
  • Loading branch information
jmprieur committed Mar 30, 2018
1 parent 791d298 commit 6b4776a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 72 deletions.
91 changes: 36 additions & 55 deletions AppCreationScripts/Configure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,7 @@
1) Run Powershell as an administrator
2) in the PowerShell window, type: Install-Module AzureAD
There are three ways to run this script
Option1 (interactive)
---------------------
Just run . .\Configue.ps1, and you will be prompted to sign-in (email address, password, and if needed MFA).
The script will be run as the signed-in user and will use the tenant in which the user is defined.
Option 2 (Interactive, but create apps in a specified tenant)
-------------------------------------------------------------
If you want to create the apps in a specific tenant, before you run this script
- In the Azure portal (https://portal.azure.com), choose your active directory tenant, then go to the Properties of the tenant and copy
the DirectoryID. This is what we'll use in this script for the tenant ID
- run . .\Configue.ps1 -TenantId [place here the GUID representing the tenant ID]
Option 2 (non-interactive)
---------------------------
This supposes that you know the credentials of the user under which identity you want to create
the applications. Here is an example of script you'd want to run in a PowerShell Window
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
. .\Configure.ps1 -Credential $mycreds
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
#>

# Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
Expand Down Expand Up @@ -55,15 +36,15 @@ Function AddResourcePermission($requiredAccess, `
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
{
# If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
if ($servicePrincipal)
{
$sp = $servicePrincipal
}
else
# If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
if ($servicePrincipal)
{
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq '$applicationDisplayName'"
}
$sp = $servicePrincipal
}
else
{
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq '$applicationDisplayName'"
}
$appid = $sp.AppId
$requiredAccess = New-Object Microsoft.Open.AzureAD.Model.RequiredResourceAccess
$requiredAccess.ResourceAppId = $appid
Expand All @@ -86,36 +67,36 @@ Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requ

Function UpdateLine([string] $line, [string] $value)
{
$index = $line.IndexOf(':')
if ($index -eq -1)
{
$index = $line.IndexOf('=')
}
if ($index -ige 0)
{
$line = $line.Substring(0, $index+1) + " """+$value + ""","
}
return $line
$index = $line.IndexOf(':')
if ($index -eq -1)
{
$index = $line.IndexOf('=')
}
if ($index -ige 0)
{
$line = $line.Substring(0, $index+1) + " """+$value + ""","
}
return $line
}

Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable] $dictionary)
{
$lines = Get-Content $configFilePath
$index = 0
while($index -lt $lines.Length)
{
$line = $lines[$index]
foreach($key in $dictionary.Keys)
{
if ($line.Contains($key))
{
$lines[$index] = UpdateLine $line $dictionary[$key]
}
}
$index++
}
Set-Content -Path $configFilePath -Value $lines -Force
$lines = Get-Content $configFilePath
$index = 0
while($index -lt $lines.Length)
{
$line = $lines[$index]
foreach($key in $dictionary.Keys)
{
if ($line.Contains($key))
{
$lines[$index] = UpdateLine $line $dictionary[$key]
}
}
$index++
}

Set-Content -Path $configFilePath -Value $lines -Force
}

Set-Content -Value "<html><body><table>" -Path createdApps.html
Expand Down Expand Up @@ -193,7 +174,7 @@ Function ConfigureApplications
Write-Host "Updating the sample code ($configFile)"
$dictionary = @{ "public static string clientId" = $clientAadApplication.AppId };
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html

}
}
Expand Down
51 changes: 34 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
services: active-directory
platforms: dotnet, xamarin
platforms: dotnet
author: jmprieur
level: 400
client: Xamarin, Desktop
Expand All @@ -24,7 +24,8 @@ This sample solution shows how to build a native application that uses Xamarin t

### Scenario

The user enters an alias in the organization of interest an presses search. If needed, the user is asked to sign in to that organization and to consent for the application to read user's basic profile. Then, if the alias is found in the user's organization, the profile of the corresponding user is written in the UI (first name, last name, email address, and phone number). The picture below shows the UI for a UWP application, but it's really the same for other platforms.
The user enters an alias in the organization of interest an presses search. If needed, the user is asked to sign in to that organization and to consent for the application to read user's basic profile.
Then, if the alias is found in the user's organization, the profile of the corresponding user is written in the UI. This profile consists in first name, last name, email address, and phone number. The picture below shows the UI for a UWP application, but it's really the same for other platforms.

![UI](./ReadmeFiles/ui.png)

Expand All @@ -49,7 +50,7 @@ For this sample, we recommend that you use the Visual Studio Integration to run

### Step 2: Clone or download this repository

Once you have completed your IDE setup, from your IDE or the command-line, run:
Once you've completed your IDE setup, from your IDE or the command-line, run:

`git clone https://github.com/Azure-Samples/active-directory-dotnet-native-multitarget.git`

Expand All @@ -64,20 +65,20 @@ For this sample, you must have at least one user homed in the AAD tenant in whic
This step and the following are optional. Indeed, the sample is configured to run with any tenant out of the box. But for best understanding, we recommend completing these two steps and registering the application in your own tenant.

1. Sign in to the [Azure portal](https://portal.azure.com).
2. Click on Active Directory in the left-hand nav.
3. Click the directory tenant where you wish to register the sample application.
4. Click the Applications tab.
5. In the drawer, click **Add**.
6. Click "Add an application my organization is developing"
7. Enter a friendly name for the application, for example "DirectorySearcherClient", select "Native Client Application", and click next.
8. Enter a Redirect Uri value of your choosing and of form `http://MyDirectorySearcherApp`.
9. While still in the Azure portal, click the Configure tab of your application.
10. Find the Client ID value and copy it aside. You will need this value later when configuring your application.
11. In the Required Permissions section:

- click on the **Add** button
- then **Select an API** and choose "Microsoft Graph" and click the **Select** Button
- then select the permissions: In the "Enable Access" pane, in the "Delegated permissions" section, check the "Sign in and read user profile" and "Read all user's basic profiles". These permissions don't require admin consent and therefore you won't require a tenant admin to approve the application. Press **Select** at the bottom of the pane, and then **Done** in the "Add API access pane"
1. On the top bar, click on your account and under the **Directory** list, choose the Active Directory tenant where you wish to register your application.
1. Click on **All services** in the left-hand nav, and choose **Azure Active Directory**.
1. In the **Azure Active Directory** pane, click on **App registrations** and choose **New application registration**.
1. Enter a friendly name for the application, for example 'MyDirectorySearcherApp' and select 'Native' as the *Application Type*.
1. For the *Redirect URI*, enter `https://<your_tenant_name>/MyDirectorySearcherApp`, replacing `<your_tenant_name>` with the name of your Azure AD tenant.
1. Click on **Create** to create the application.
1. In the succeeding page, Find the *Application ID* value and copy it to the clipboard. You'll need it to configure the Visual Studio configuration file for this project.
1. Then click on **Settings**, and choose **Properties**.
1. For the App ID URI, replace the guid in the generated URI 'https://\<your_tenant_name\>/\<guid\>', with the name of your service, for example, 'https://\<your_tenant_name\>/MyDirectorySearcherApp' (replacing `<your_tenant_name>` with the name of your Azure AD tenant)
1. Configure Permissions for your application. To that extent, in the Settings menu, choose the 'Required permissions' section and then,
click on **Add**, then **Select an API**, and type `Microsoft Graph` in the textbox.
Then, click on **Select Permissions** and select the permissions:
In the "Enable Access" pane, in the "Delegated permissions" section, check the "Sign in and read user profile" and "Read all user's basic profiles".
These permissions don't require admin consent and therefore you won't require a tenant admin to approve the application. Press **Select** at the bottom of the pane, and then **Done** in the "Add API access pane"

### Step 5: Configure the sample to use your Azure AD tenant

Expand Down Expand Up @@ -106,6 +107,22 @@ The `DirectorySearcherLib` PCL is the application's shared C# code base, which c

Then, the application queries the Microsoft Graph API for information about a user with a matching alias, in the authenticated user's tenant. When the user is found, it's returned to the platform-specific UI code. By writing both the identity and search logic in the `DirectorySearcherLib` PCL, the code only needs to be written once and can be reused across each platform.

## Community Help and Support

Use [Stack Overflow](http://stackoverflow.com/questions/tagged/adal) to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [`adal` `dotnet`].

If you find and bug in the sample, please raise the issue on [GitHub Issues](../../issues).

To provide a recommendation, visit the following [User Voice page](https://feedback.azure.com/forums/169401-azure-active-directory).

## Contributing

If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md).

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

## More information

For more information, see ADAL.NET's conceptual documentation:
Expand Down

0 comments on commit 6b4776a

Please sign in to comment.