@@ -8,7 +8,7 @@ keywords: intune graphapi c# powershell permission roles
88author : dougeby
99manager : dougeby
1010ms.author : dougeby
11- ms.date : 01/10 /2022
11+ ms.date : 02/28 /2022
1212ms.topic : overview
1313ms.service : microsoft-intune
1414ms.subservice : developer
@@ -361,7 +361,7 @@ This example shows how to use C# to retrieve a list of devices associated with y
361361
362362 <img src =" ../media/aad-auth-cpp-new-console.png " width =" 624 " height =" 433 " alt =" Creating a C# console app project in Visual Studio " />
363363
364- 3 . Use the Solution Explorer to add the Microsoft ADAL NuGet package to the project:
364+ 3 . Use the Solution Explorer to add the Microsoft MSAL NuGet package to the project:
365365
366366 1 . Right-click the Solution Explorer.
367367 1 . Choose ** Manage NuGet Packages…** > ; ** Browse** .
@@ -459,114 +459,7 @@ namespace IntuneGraphExample
459459
460460### Authenticate Azure AD (PowerShell)
461461
462- The following PowerShell script uses the AzureAD PowerShell module for authentication . To learn more , see [Azure Active Directory PowerShell Version 2 ](/ powershell / azure / active - directory / install - adv2 ) and the [Intune PowerShell examples ](https :// github.com/microsoftgraph/powershell-intune-samples).
463-
464- In this example , update the value of `$clientID ` to match a valid application ID .
465-
466- ``` powershell
467- function Get - AuthToken {
468- [cmdletbinding ()]
469- param
470- (
471- [Parameter (Mandatory = $true )]
472- $User
473- )
474-
475- $userUpn = New - Object " System.Net.Mail.MailAddress" - ArgumentList $User
476- $tenant = $userUpn .Host
477-
478- Write - Host " Checking for AzureAD module..."
479-
480- $AadModule = Get - Module - Name " AzureAD" - ListAvailable
481- if ($AadModule - eq $null ) {
482- Write - Host " AzureAD PowerShell module not found, looking for AzureADPreview"
483- $AadModule = Get - Module - Name " AzureADPreview" - ListAvailable
484- }
485-
486- if ($AadModule - eq $null ) {
487- write - host
488- write - host " AzureAD Powershell module not installed..." - f Red
489- write - host " Install by running 'Install-Module AzureAD' or 'Install-Module AzureADPreview' from an elevated PowerShell prompt" - f Yellow
490- write - host " Script can't continue..." - f Red
491- write - host
492- exit
493- }
494-
495- # Getting path to ActiveDirectory Assemblies
496- # If the module count is greater than 1 find the latest version
497-
498- if ($AadModule .count - gt 1 ) {
499- $Latest_Version = ($AadModule | select version | Sort - Object )[- 1 ]
500- $aadModule = $AadModule | ? { $_ .version - eq $Latest_Version .version }
501- $adal = Join - Path $AadModule .ModuleBase " Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
502- $adalforms = Join - Path $AadModule .ModuleBase " Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
503- }
504-
505- else {
506- $adal = Join - Path $AadModule .ModuleBase " Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
507- $adalforms = Join - Path $AadModule .ModuleBase " Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
508- }
509-
510- [System .Reflection .Assembly ]:: LoadFrom ($adal ) | Out - Null
511- [System .Reflection .Assembly ]:: LoadFrom ($adalforms ) | Out - Null
512-
513- $clientId = " <Your Application ID>"
514- $redirectUri = " urn:ietf:wg:oauth:2.0:oob"
515- $resourceAppIdURI = " https://graph.microsoft.com"
516- $authority = " https://login.microsoftonline.com/$Tenant"
517-
518- try {
519- $authContext = New - Object " Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" - ArgumentList $authority
520- # https://msdn.microsoft.com/library/azure/microsoft.identitymodel.clients.activedirectory.promptbehavior.aspx
521- # Change the prompt behaviour to force credentials each time: Auto, Always, Never, RefreshSession
522- $platformParameters = New - Object " Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" - ArgumentList " Auto"
523- $userId = New - Object " Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier" - ArgumentList ($User , " OptionalDisplayableId" )
524- $authResult = $authContext .AcquireTokenAsync ($resourceAppIdURI , $clientId , $redirectUri , $platformParameters , $userId ).Result
525- # If the accesstoken is valid then create the authentication header
526- if ($authResult .AccessToken ) {
527- # Creating header for Authorization token
528- $authHeader = @{
529- 'Content-Type' = 'application/json'
530- 'Authorization' = " Bearer " + $authResult .AccessToken
531- 'ExpiresOn' = $authResult .ExpiresOn
532- }
533- return $authHeader
534- }
535- else {
536- Write - Host
537- Write - Host " Authorization Access Token is null, please re-run authentication..." - ForegroundColor Red
538- Write - Host
539- break
540- }
541- }
542- catch {
543- write - host $_ .Exception .Message - f Red
544- write - host $_ .Exception .ItemName - f Red
545- write - host
546- break
547- }
548- }
549-
550- $authToken = Get - AuthToken - User " <Your AAD Username>"
551-
552- try {
553- $uri = " https://graph.microsoft.com/beta/me/managedDevices"
554- Write - Verbose $uri
555- (Invoke - RestMethod - Uri $uri –Headers $authToken –Method Get ).Value
556- }
557- catch {
558- $ex = $_ .Exception
559- $errorResponse = $ex .Response .GetResponseStream ()
560- $reader = New - Object System .IO .StreamReader ($errorResponse )
561- $reader .BaseStream .Position = 0
562- $reader .DiscardBufferedData ()
563- $responseBody = $reader .ReadToEnd ();
564- Write - Host " Response content:`n$responseBody" - f Red
565- Write - Error " Request to $Uri failed with HTTP Status $($ex.Response.StatusCode) $($ex.Response.StatusDescription)"
566- write - host
567- break
568- }
569- ```
462+ PowerShell scripts can use the AzureAD PowerShell module for authentication . To learn more , see [Azure Active Directory PowerShell Version 2 ](/ powershell / azure / active - directory / install - adv2 ) and the [Intune PowerShell examples ](https :// github.com/microsoftgraph/powershell-intune-samples).
570463
571464## Support multiple tenants and partners
572465
0 commit comments