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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

gautamdsheth
Copy link
Collaborator

Type

  • Bug Fix
  • New Feature
  • Sample

Related Issues?

Fixes #4983

What is in this Pull Request ?

Additional fixes for cloudshell issue

@gautamdsheth gautamdsheth requested a review from Copilot July 4, 2025 11:40
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds Azure Cloud Shell detection and customizes device login prompts when running in Cloud Shell.

  • Introduces PSUtility.IsAzureCloudShell to detect the Cloud Shell environment.
  • Updates device login workflows in AzureAuthHelper, PnPConnection, and registration commands to show simplified instructions in Cloud Shell.
  • Wraps clipboard operations in try/catch and conditionally opens a browser only when not in Cloud Shell.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Commands/Utilities/PSUtility.cs Add IsAzureCloudShell helper
src/Commands/Utilities/AzureAuthHelper.cs Conditionally adjust device login prompts for Cloud Shell
src/Commands/Base/PnPConnection.cs Apply Cloud Shell‐specific prompt adjustments
src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs Apply Cloud Shell prompts in Entra ID registration flow
src/Commands/AzureAD/RegisterAzureADApp.cs Apply Cloud Shell prompts in Azure AD registration flow
Comments suppressed due to low confidence (3)

src/Commands/Utilities/PSUtility.cs:54

  • Add an XML doc comment above this method to explain its purpose and the expected environment variable, improving discoverability and maintenance.
        public static bool IsAzureCloudShell()

src/Commands/Utilities/PSUtility.cs:54

  • Consider adding unit tests for IsAzureCloudShell to verify behavior when the POWERSHELL_DISTRIBUTION_CHANNEL variable is unset, empty, or set to "CloudShell" (in various cases).
        public static bool IsAzureCloudShell()

src/Commands/Base/PnPConnection.cs:282

  • Indentation and brace alignment around the new Cloud Shell conditional block is inconsistent. Align braces and indentation with surrounding code to improve readability.
                 {

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.

Comment on lines +37 to 53
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);
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Unable to connect to PNP with CloudShell
1 participant