diff --git a/Commands/Base/ConnectOnline.cs b/Commands/Base/ConnectOnline.cs index 8f1942bec..cb44f3a2e 100644 --- a/Commands/Base/ConnectOnline.cs +++ b/Commands/Base/ConnectOnline.cs @@ -487,6 +487,11 @@ public class ConnectOnline : PSCmdlet [Parameter(Mandatory = false, ParameterSetName = ParameterSet_HIGHTRUST_CERT, HelpMessage = "The IssuerID under which the certificate has been registered in SharePoint as a Trusted Security Token issuer to use for the High Trust connection. Uses the ClientID if not specified.")] [Parameter(Mandatory = false, ParameterSetName = ParameterSet_HIGHTRUST_PFX, HelpMessage = "The IssuerID under which the CER counterpart of the PFX has been registered in SharePoint as a Trusted Security Token issuer to use for the High Trust connection. Uses the ClientID if not specified.")] public string HighTrustCertificateIssuerId; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_HIGHTRUST_CERT, HelpMessage = "Name of the user (login name) on whose behalf to create the access token. Supported input formats are SID and User Principal Name (UPN). If the parameter is not specified, an App Only Context is created.")] + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_HIGHTRUST_PFX, HelpMessage = "Name of the user (login name) on whose behalf to create the access token. Supported input formats are SID and User Principal Name (UPN). If the parameter is not specified, an App Only Context is created.")] + [ValidateNotNullOrEmpty()] + public string UserName; #endif protected override void ProcessRecord() @@ -622,11 +627,36 @@ protected override void ProcessRecord() #if ONPREMISES else if (ParameterSetName == ParameterSet_HIGHTRUST_CERT) { - connection = SPOnlineConnectionHelper.InstantiateHighTrustConnection(Url, ClientId, HighTrustCertificate, HighTrustCertificateIssuerId ?? ClientId, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, Host, NoTelemetry, SkipTenantAdminCheck); + connection = SPOnlineConnectionHelper.InstantiateHighTrustConnection(Url, + ClientId, + HighTrustCertificate, + HighTrustCertificateIssuerId ?? ClientId, + MinimalHealthScore, + RetryCount, + RetryWait, + RequestTimeout, + TenantAdminUrl, + Host, + NoTelemetry, + SkipTenantAdminCheck, + UserName); } else if (ParameterSetName == ParameterSet_HIGHTRUST_PFX) { - connection = SPOnlineConnectionHelper.InstantiateHighTrustConnection(Url, ClientId, HighTrustCertificatePath, HighTrustCertificatePassword, HighTrustCertificateIssuerId ?? ClientId, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, Host, NoTelemetry, SkipTenantAdminCheck); + connection = SPOnlineConnectionHelper.InstantiateHighTrustConnection(Url, + ClientId, + HighTrustCertificatePath, + HighTrustCertificatePassword, + HighTrustCertificateIssuerId ?? ClientId, + MinimalHealthScore, + RetryCount, + RetryWait, + RequestTimeout, + TenantAdminUrl, + Host, + NoTelemetry, + SkipTenantAdminCheck, + UserName); } #endif else @@ -639,7 +669,18 @@ protected override void ProcessRecord() creds = Host.UI.PromptForCredential(Properties.Resources.EnterYourCredentials, "", "", ""); } } - connection = SPOnlineConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), creds, Host, CurrentCredentials, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, NoTelemetry, SkipTenantAdminCheck, AuthenticationMode); + connection = SPOnlineConnectionHelper.InstantiateSPOnlineConnection(new Uri(Url), + creds, + Host, + CurrentCredentials, + MinimalHealthScore, + RetryCount, + RetryWait, + RequestTimeout, + TenantAdminUrl, + NoTelemetry, + SkipTenantAdminCheck, + AuthenticationMode); } #if !ONPREMISES #if !NETSTANDARD2_0 diff --git a/Commands/Base/SPOnlineConnectionHelper.cs b/Commands/Base/SPOnlineConnectionHelper.cs index cef42dc2e..0e9231c7b 100644 --- a/Commands/Base/SPOnlineConnectionHelper.cs +++ b/Commands/Base/SPOnlineConnectionHelper.cs @@ -83,18 +83,18 @@ internal static SPOnlineConnection InstantiateSPOnlineConnection(Uri url, string #if !NETSTANDARD2_0 #if ONPREMISES - internal static SPOnlineConnection InstantiateHighTrustConnection(string url, string clientId, string hightrustCertificatePath, string hightrustCertificatePassword, string hightrustCertificateIssuerId, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, PSHost host, bool disableTelemetry, bool skipAdminCheck) + internal static SPOnlineConnection InstantiateHighTrustConnection(string url, string clientId, string hightrustCertificatePath, string hightrustCertificatePassword, string hightrustCertificateIssuerId, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, PSHost host, bool disableTelemetry, bool skipAdminCheck, string loginName) { var authManager = new OfficeDevPnP.Core.AuthenticationManager(); - var context = authManager.GetHighTrustCertificateAppOnlyAuthenticatedContext(url, clientId, hightrustCertificatePath, hightrustCertificatePassword, hightrustCertificateIssuerId); + var context = authManager.GetHighTrustCertificateAppAuthenticatedContext(url, clientId, hightrustCertificatePath, hightrustCertificatePassword, hightrustCertificateIssuerId, loginName); return InstantiateHighTrustConnection(context, url, minimalHealthScore, retryCount, retryWait, requestTimeout, tenantAdminUrl, host, disableTelemetry, skipAdminCheck); } - internal static SPOnlineConnection InstantiateHighTrustConnection(string url, string clientId, System.Security.Cryptography.X509Certificates.X509Certificate2 hightrustCertificate, string hightrustCertificateIssuerId, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, PSHost host, bool disableTelemetry, bool skipAdminCheck) + internal static SPOnlineConnection InstantiateHighTrustConnection(string url, string clientId, System.Security.Cryptography.X509Certificates.X509Certificate2 hightrustCertificate, string hightrustCertificateIssuerId, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, PSHost host, bool disableTelemetry, bool skipAdminCheck, string loginName) { var authManager = new OfficeDevPnP.Core.AuthenticationManager(); - var context = authManager.GetHighTrustCertificateAppOnlyAuthenticatedContext(url, clientId, hightrustCertificate, hightrustCertificateIssuerId); + var context = authManager.GetHighTrustCertificateAppAuthenticatedContext(url, clientId, hightrustCertificate, hightrustCertificateIssuerId, loginName); return InstantiateHighTrustConnection(context, url, minimalHealthScore, retryCount, retryWait, requestTimeout, tenantAdminUrl, host, disableTelemetry, skipAdminCheck); } @@ -114,7 +114,18 @@ private static SPOnlineConnection InstantiateHighTrustConnection(ClientContext c connectionType = ConnectionType.TenantAdmin; } } - return new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, null, url, tenantAdminUrl, PnPPSVersionTag, host, disableTelemetry, InitializationType.HighTrust); + return new SPOnlineConnection(context, + connectionType, + minimalHealthScore, + retryCount, + retryWait, + null, + url, + tenantAdminUrl, + PnPPSVersionTag, + host, + disableTelemetry, + InitializationType.HighTrust); } #endif #endif