Skip to content

Add AzureCloudShell detection and improve user prompts for device login #4997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Commands/AzureAD/RegisterAzureADApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,22 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
{
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
{
try
if (PSUtility.IsAzureCloudShell())
{
ClipboardService.SetText(deviceCodeResult.UserCode);
Host.UI.WriteWarningLine($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
}
catch
else
{
try
{
ClipboardService.SetText(deviceCodeResult.UserCode);
}
catch
{
}
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
}
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
return Task.FromResult(0);
}, AzureEnvironment))
{
Expand Down
17 changes: 12 additions & 5 deletions src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,22 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
{
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
{
try
if (PSUtility.IsAzureCloudShell())
{
ClipboardService.SetText(deviceCodeResult.UserCode);
Host.UI.WriteWarningLine($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
}
catch
else
{
try
{
ClipboardService.SetText(deviceCodeResult.UserCode);
}
catch
{
}
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
}
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
return Task.FromResult(0);
}, AzureEnvironment))
{
Expand Down
17 changes: 12 additions & 5 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,22 @@ internal static PnPConnection CreateWithDeviceLogin(string clientId, string url,
{
authManager = Framework.AuthenticationManager.CreateWithDeviceLogin(clientId, tenantId, (deviceCodeResult) =>
{
try
if (PSUtility.IsAzureCloudShell())
{
ClipboardService.SetText(deviceCodeResult.UserCode);
messageWriter.LogWarning($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.\n\n");
}
catch
else
{
try
{
ClipboardService.SetText(deviceCodeResult.UserCode);
}
catch
{
}
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");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
}
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");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);

return Task.FromResult(0);
}, azureEnvironment, tokenCacheCallback: async (tokenCache) =>
Expand Down
22 changes: 17 additions & 5 deletions src/Commands/Utilities/AzureAuthHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,24 @@ internal static string AuthenticateDeviceLogin(CancellationTokenSource cancellat
{
try
{
using (var authManager = PnP.Framework.AuthenticationManager.CreateWithDeviceLogin(CLIENTID, (result) =>
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(CLIENTID, (deviceCodeResult) =>
{

ClipboardService.SetText(result.UserCode);
messageWriter.LogWarning($"Please login.\n\nWe opened a browser and navigated to {result.VerificationUrl}\n\nEnter code: {result.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(result.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
if (PSUtility.IsAzureCloudShell())
{
messageWriter.LogWarning($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
}
else
{
try
{
ClipboardService.SetText(deviceCodeResult.UserCode);
}
catch
{
}
messageWriter.LogWarning($"Please login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
}

Comment on lines +37 to 53
Copy link
Preview

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Device login prompt logic is duplicated in multiple places. Extract the Cloud Shell vs. local logic into a shared helper method to reduce duplication and keep messaging consistent.

Suggested change
if (PSUtility.IsAzureCloudShell())
{
messageWriter.LogWarning($"\n\nTo sign in, use a web browser to open the page {deviceCodeResult.VerificationUrl} and enter the code {deviceCodeResult.UserCode} to authenticate.");
}
else
{
try
{
ClipboardService.SetText(deviceCodeResult.UserCode);
}
catch
{
}
messageWriter.LogWarning($"Please login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
}
HandleDeviceLoginPrompt(deviceCodeResult, messageWriter, cancellationTokenSource);

Copilot uses AI. Check for mistakes.

return Task.FromResult(0);
}, azureEnvironment))
Expand Down
11 changes: 11 additions & 0 deletions src/Commands/Utilities/PSUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,16 @@ public static bool IsUserLocalAdmin()
return true;
}
#pragma warning restore CA1416 // Validate platform compatibility

public static bool IsAzureCloudShell()
{
string psDistChannel = Environment.GetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL");
if (string.IsNullOrWhiteSpace(psDistChannel))
{
return false;
}

return psDistChannel == "CloudShell";
Copy link
Preview

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a case‐insensitive comparison to avoid issues if the environment variable uses a different case. For example: return string.Equals(psDistChannel, "CloudShell", StringComparison.OrdinalIgnoreCase);

Suggested change
return psDistChannel == "CloudShell";
return string.Equals(psDistChannel, "CloudShell", StringComparison.OrdinalIgnoreCase);

Copilot uses AI. Check for mistakes.

}
}
}
Loading