Skip to content
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

Fix #1180 - issue with PnP Management shell access in non-commercial clouds #2437

Merged
merged 5 commits into from
Oct 10, 2022
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `Set-PnPTenant` cmdlet not working when `-Force` parameter is specified. [#2373](https://github.com/pnp/powershell/pull/2373)
- Fixed `Add-PnPTeamsTab` cmdlet not working with certain types when using dynamic parameters. [#2405](https://github.com/pnp/powershell/pull/2405)
- Fixed `Get-PnPVivaConnectionsDashboardACE` missing the `isVisible` property under `CardButtonActions` causing using `Update-PnPVivaConnectionsDashboardACE` to hide card buttons [#2433](https://github.com/pnp/powershell/pull/2433
- Fixed issue with `Set-PnPTeamsChannel -IsFavoriteByDefault` throwing a `Nullable object must have a value` under certain circumstances [#2425](https://github.com/pnp/powershell/pull/2425)
- Fixed issue with `Set-PnPTeamsChannel -IsFavoriteByDefault` throwing a `Nullable object must have a value` under certain circumstances [#2425](https://github.com/pnp/powershell/pull/2425]
- Fixed `Register-PnPManagementShellAccess` for non-commercial cloud environment. Users must enter the tenant name if the environment is a non-commercial cloud environment. [#2437](https://github.com/pnp/powershell/pull/2437)
- Fixed issue with writing warning or error messages in Azure automation or screens with small width. [#2438](https://github.com/pnp/powershell/pull/2438)

### Contributors
Expand Down
21 changes: 13 additions & 8 deletions src/Commands/AzureAD/RegisterManagementShellAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ public class RegisterManagementShellAccess : PSCmdlet
public SwitchParameter ShowConsentUrl;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SHOWURL)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_REGISTER)]
public string TenantName;

protected override void ProcessRecord()
{
source = new CancellationTokenSource();
var messageWriter = new CmdletMessageWriter(this);
CancellationToken cancellationToken = source.Token;

var endPoint = string.Empty;
using (var authManager = new AuthenticationManager())
{
endPoint = authManager.GetAzureADLoginEndPoint(AzureEnvironment);
}

if (AzureEnvironment != AzureEnvironment.Production && string.IsNullOrEmpty(TenantName))
{
WriteWarning("Please specify the Tenant name for non-commercial clouds, otherwise this operation will fail.");
}

Task.Factory.StartNew(() =>
{
if (ParameterSetName == ParameterSet_REGISTER)
Expand All @@ -55,11 +60,11 @@ protected override void ProcessRecord()
},
successMessageHtml: $"You successfully consented the PnP Management Shell Application for use by PnP PowerShell. Feel free to close this window.",
failureMessageHtml: $"You did not consent for the PnP Management Shell Application for use by PnP PowerShell. Feel free to close this browser window.",
azureEnvironment: AzureEnvironment))
azureEnvironment: AzureEnvironment, tenantId: TenantName))
{
try
{
authManager.GetAccessTokenAsync(new[] { $"https://{GetGraphEndPoint()}/.default" }, cancellationToken, Microsoft.Identity.Client.Prompt.Consent).GetAwaiter().GetResult();
authManager.GetAccessTokenAsync(new[] { $"https://{GetGraphEndPoint()}/.default" }, source.Token, Microsoft.Identity.Client.Prompt.Consent).GetAwaiter().GetResult();
}
catch (Microsoft.Identity.Client.MsalException)
{
Expand All @@ -71,7 +76,7 @@ protected override void ProcessRecord()
{
if (!string.IsNullOrEmpty(TenantName))
{
messageWriter.WriteMessage($"Share the following URL with a person that has appropriate access rights on the Azure AD to grant consent for Application Registrations:\n\nhttps://login.microsoftonline.com/{TenantName}/adminconsent?client_id={PnPConnection.PnPManagementShellClientId}");
messageWriter.WriteMessage($"Share the following URL with a person that has appropriate access rights on the Azure AD to grant consent for Application Registrations:\n\n{endPoint}/{TenantName}/adminconsent?client_id={PnPConnection.PnPManagementShellClientId}");
}
else
{
Expand All @@ -87,7 +92,7 @@ protected override void ProcessRecord()
var accessToken = string.Empty;
try
{
accessToken = authManager.GetAccessTokenAsync(new[] { $"https://{GetGraphEndPoint()}/.default" }, cancellationToken).GetAwaiter().GetResult();
accessToken = authManager.GetAccessTokenAsync(new[] { $"https://{GetGraphEndPoint()}/.default" }, source.Token).GetAwaiter().GetResult();
}
catch (Microsoft.Identity.Client.MsalException)
{
Expand Down Expand Up @@ -123,7 +128,7 @@ protected override void ProcessRecord()
}
}
}
messageWriter.WriteMessage($"Share the following URL with a person that has appropriate access rights on the Azure AD to grant consent for Application Registrations:\n\nhttps://login.microsoftonline.com/{tenantId}/adminconsent?client_id={PnPConnection.PnPManagementShellClientId}");
messageWriter.WriteMessage($"Share the following URL with a person that has appropriate access rights on the Azure AD to grant consent for Application Registrations:\n\n{endPoint}/{tenantId}/adminconsent?client_id={PnPConnection.PnPManagementShellClientId}");
if (tenantId == "{M365-Tenant-Id}")
{
messageWriter.WriteMessage($"To get M365-Tenant-Id value, use the Get-PnPTenantId cmdlet:\nhttps://pnp.github.io/powershell/cmdlets/Get-PnPTenantId.html");
Expand All @@ -132,7 +137,7 @@ protected override void ProcessRecord()
}
}
messageWriter.Finished = true;
}, cancellationToken);
}, source.Token);
messageWriter.Start();
}

Expand All @@ -146,4 +151,4 @@ private string GetGraphEndPoint()
return PnP.Framework.AuthenticationManager.GetGraphEndPoint(AzureEnvironment);
}
}
}
}