Skip to content

Commit 63e0fea

Browse files
authored
Merge pull request #2036 from microsoftgraph/po/LogMsalEvents
Log MSAL events to debug stream
2 parents 7329697 + ed13246 commit 63e0fea

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
// ------------------------------------------------------------------------------
44
using Azure.Core;
5+
using Azure.Core.Diagnostics;
56
using Azure.Identity;
67
using Microsoft.Graph.PowerShell.Authentication.Core.Extensions;
78
using Microsoft.Identity.Client;
89
using Microsoft.Identity.Client.Extensions.Msal;
910
using System;
11+
using System.Diagnostics.Tracing;
1012
using System.Globalization;
1113
using System.IO;
1214
using System.Linq;
@@ -201,8 +203,14 @@ public static async Task<IAuthContext> AuthenticateAsync(IAuthContext authContex
201203
{
202204
try
203205
{
204-
signInAuthContext = await SignInAsync(authContext, cancellationToken).ConfigureAwait(false);
205-
retrySignIn = false;
206+
// Write MSAL logs to debug stream.
207+
using (AzureEventSourceListener listener = new AzureEventSourceListener(
208+
(args, message) => GraphSession.Instance.OutputWriter.WriteDebug($"{message}"),
209+
level: EventLevel.Informational))
210+
{
211+
signInAuthContext = await SignInAsync(authContext, cancellationToken).ConfigureAwait(false);
212+
retrySignIn = false;
213+
};
206214
}
207215
catch (AuthenticationFailedException authEx)
208216
{

src/Authentication/Authentication/test/Connect-MgGraph.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,17 @@ Describe 'Connect-MgGraph In App Mode' {
9595
}
9696

9797
}
98+
9899
Describe 'Connect-MgGraph Dependency Resolution' {
99100
It 'Should load Mg module side by side with Az module.' {
100101
{ Connect-AzAccount -ApplicationId $RandomClientId -CertificateThumbprint "Invalid" -Tenant "Invalid" -ErrorAction Stop } | Should -Throw -ExpectedMessage "*Could not find tenant id*"
101102
{ Connect-MgGraph -TenantId "thisdomaindoesnotexist.com" -ErrorAction Stop -UseDeviceAuthentication } | Should -Throw -ExpectedMessage "*AADSTS90002*"
102103
}
104+
}
105+
106+
Describe 'Connect-MgGraph Logging' {
107+
It 'Should write MSAL logs to debug stream.' {
108+
$MgDebugStream = $(Connect-MgGraph -TenantId "thisdomaindoesnotexist.com" -UseDeviceAuthentication -Debug -ErrorAction SilentlyContinue) 5>&1
109+
$MgDebugStream[0] | Should -Match "DeviceCodeCredential.Authenticate invoked. Scopes: \[ User.Read \]"
110+
}
103111
}

0 commit comments

Comments
 (0)