|
3 | 3 | using System.IO;
|
4 | 4 | using System.Net.Http;
|
5 | 5 | using System.Threading.Tasks;
|
| 6 | +using GitCredentialManager.Interop.Windows.Native; |
6 | 7 | using Microsoft.Identity.Client;
|
7 | 8 | using Microsoft.Identity.Client.Extensions.Msal;
|
8 | 9 |
|
@@ -223,12 +224,25 @@ private async Task<IPublicClientApplication> CreatePublicClientApplicationAsync(
|
223 | 224 | appBuilder.WithLogging(OnMsalLogMessage, LogLevel.Verbose, enablePiiLogging, false);
|
224 | 225 | }
|
225 | 226 |
|
226 |
| - // If we have a parent window ID we should tell MSAL about it so it can parent any authentication dialogs |
227 |
| - // correctly. We only support this on Windows right now as MSAL only supports embedded/dialogs on Windows. |
228 |
| - if (PlatformUtils.IsWindows() && !string.IsNullOrWhiteSpace(Context.Settings.ParentWindowId) && |
229 |
| - int.TryParse(Context.Settings.ParentWindowId, out int hWndInt) && hWndInt > 0) |
| 227 | + // On Windows we should set the parent window handle for the authentication dialogs |
| 228 | + // so that they are displayed as a child of the correct window. |
| 229 | + if (PlatformUtils.IsWindows()) |
230 | 230 | {
|
231 |
| - appBuilder.WithParentActivityOrWindow(() => new IntPtr(hWndInt)); |
| 231 | + // If we have a parent window ID then use that, otherwise use the hosting terminal window. |
| 232 | + IntPtr parentHandle; |
| 233 | + if (!string.IsNullOrWhiteSpace(Context.Settings.ParentWindowId) && |
| 234 | + int.TryParse(Context.Settings.ParentWindowId, out int hWndInt) && hWndInt > 0) |
| 235 | + { |
| 236 | + parentHandle = new IntPtr(hWndInt); |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + IntPtr consoleHandle = Kernel32.GetConsoleWindow(); |
| 241 | + parentHandle = User32.GetAncestor(consoleHandle, GetAncestorFlags.GetRootOwner); |
| 242 | + } |
| 243 | + |
| 244 | + Context.Trace.WriteLine($"Using parent window ID '{parentHandle}' for MSAL authentication dialogs."); |
| 245 | + appBuilder.WithParentActivityOrWindow(() => parentHandle); |
232 | 246 | }
|
233 | 247 |
|
234 | 248 | // Configure the broker if enabled
|
|
0 commit comments