Skip to content

Commit 9f6529e

Browse files
committed
Fixing devicelogin not working on a desktopless Linux device
1 parent 9508fee commit 9f6529e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

documentation/Connect-PnPOnline.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Connect-PnPOnline [-ReturnConnection] [-Url] <String> [-CreateDrive] [-DriveName
5656

5757
### DeviceLogin
5858
```powershell
59-
Connect-PnPOnline [-ReturnConnection] [-Url] <String> [-PersistLogin] [-CreateDrive] [-DriveName <String>] [-DeviceLogin] -Tenant <String>
59+
Connect-PnPOnline [-ReturnConnection] [-Url] <String> [-PersistLogin] [-CreateDrive] [-DriveName <String>] -DeviceLogin -Tenant <String>
6060
[-ClientId <String>] [-AzureEnvironment <AzureEnvironment>]
6161
[-ValidateConnection] [-MicrosoftGraphEndPoint <string>]
6262
[-AzureADLoginEndPoint <string>] [-Connection <PnPConnection>]

src/Commands/Base/PnPConnection.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,34 @@ internal static PnPConnection CreateWithDeviceLogin(string clientId, string url,
286286
}
287287
else
288288
{
289+
var copiedToClipboard = true;
289290
try
290291
{
291292
ClipboardService.SetText(deviceCodeResult.UserCode);
292293
}
293294
catch
294295
{
296+
copiedToClipboard = false;
297+
}
298+
299+
var browserOpened = true;
300+
try
301+
{
302+
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
303+
}
304+
catch
305+
{
306+
browserOpened = false;
307+
}
308+
309+
if (copiedToClipboard && browserOpened)
310+
{
311+
messageWriter.LogWarning($"\n\nCode {deviceCodeResult.UserCode} has been copied to your clipboard and a new tab in the browser has been opened. Please paste this code in there and proceed.\n\n");
312+
}
313+
else
314+
{
315+
messageWriter.LogWarning($"\n\nOpen a browser, navigate to {deviceCodeResult.VerificationUrl} and authenticate using code {deviceCodeResult.UserCode} to proceed.\n\n");
295316
}
296-
messageWriter.LogWarning($"\n\nCode {deviceCodeResult.UserCode} has been copied to your clipboard and a new tab in the browser has been opened. Please paste this code in there and proceed.\n\n");
297-
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
298317
}
299318

300319
return Task.FromResult(0);

0 commit comments

Comments
 (0)