internal static Action <DeviceCodeResult> DeviceLoginCallback(PSHost host, bool launchBrowser) { return(deviceCodeResult => { if (launchBrowser) { Utilities.Clipboard.CopyNew(deviceCodeResult.UserCode); host?.UI.WriteLine($"Code {deviceCodeResult.UserCode} has been copied to clipboard"); #if !NETSTANDARD2_1 BrowserHelper.LaunchBrowser(deviceCodeResult.VerificationUrl, (success) => { }); #else OpenBrowser(returnData["verification_url"]); messageCallback(returnData["message"]); var tokenResult = GetTokenResult(connectionUri, returnData, messageCallback, progressCallback, cancelRequest); if (tokenResult != null) { progressCallback("Token received"); spoConnection = new PnPConnection(tokenResult, ConnectionMethod.GraphDeviceLogin, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag, host, disableTelemetry, InitializationType.GraphDeviceLogin); } else { progressCallback("No token received."); } #endif } else { host?.UI.WriteLine(deviceCodeResult.Message); } }); }
internal static bool DisconnectProvidedService(PnPConnection connection) { connection.ClearTokens(); Environment.SetEnvironmentVariable("PNPPSHOST", string.Empty); Environment.SetEnvironmentVariable("PNPPSSITE", string.Empty); if (connection == null) { return(false); } connection.Context = null; connection = null; return(true); }
protected override void ProcessRecord() { // If no specific connection has been passed in, take the connection from the current context if (Connection == null) { Connection = PnPConnection.CurrentConnection; } #if !ONPREMISES if (Connection?.Certificate != null) { #if !NETSTANDARD2_1 if (Connection != null && Connection.DeleteCertificateFromCacheOnDisconnect) { PnPConnectionHelper.CleanupCryptoMachineKey(Connection.Certificate); } #endif Connection.Certificate = null; } #endif var success = false; if (Connection != null) { success = DisconnectProvidedService(Connection); } else { success = DisconnectCurrentService(); } if (!success) { throw new InvalidOperationException(Properties.Resources.NoConnectionToDisconnect); } var provider = SessionState.Provider.GetAll().FirstOrDefault(p => p.Name.Equals(SPOProvider.PSProviderName, StringComparison.InvariantCultureIgnoreCase)); if (provider != null) { //ImplementingAssembly was introduced in Windows PowerShell 5.0. #if !NETSTANDARD2_1 var drives = Host.Version.Major >= 5 ? provider.Drives.Where(d => d.Provider.Module.ImplementingAssembly.FullName == Assembly.GetExecutingAssembly().FullName) : provider.Drives; #else var drives = Host.Version.Major >= 5 ? provider.Drives.Where(d => d.Provider.Module.Name == Assembly.GetExecutingAssembly().FullName) : provider.Drives; #endif foreach (var drive in drives) { SessionState.Drive.Remove(drive.Name, true, "Global"); } } }
/// <summary> /// Returns a PnPConnection based on connecting using an existing token /// </summary> /// <param name="token">Token to connect with</param> /// <param name="tokenAudience">Indicator of <see cref="TokenAudience"/> indicating for which API this token is meant to be used</param> /// <param name="host">PowerShell Host environment in which the commands are being run</param> /// <param name="initializationType">Indicator of type <see cref="InitializationType"/> which indicates the method used to set up the connection. Used for gathering usage analytics.</param> /// <param name="url">Url of the SharePoint environment to connect to, if applicable. Leave NULL not to connect to a SharePoint environment.</param> /// <param name="clientContext">A SharePoint ClientContext to make available within this connection. Leave NULL to not connect to a SharePoint environment.</param> /// <param name="minimalHealthScore">Minimum health score that the SharePoint server should report before allowing requests to be executed on it. Scale of 0 to 10 where 0 is healthiest and 10 is least healthy. Leave NULL not to perform health checks on SharePoint.</param> /// <param name="pnpVersionTag">Identifier set on the SharePoint ClientContext as the ClientTag to identify the source of the requests to SharePoint. Leave NULL not to set it.</param> /// <param name="disableTelemetry">Boolean indicating if telemetry on the commands being executed should be disabled. Telemetry is enabled by default.</param> /// <returns><see cref="PnPConnection"/ instance which can be used to communicate with one of the supported APIs</returns> public static PnPConnection GetConnectionWithToken(GenericToken token, TokenAudience tokenAudience, PSHost host, InitializationType initializationType, string url = null, ClientContext clientContext = null, int?minimalHealthScore = null, string pnpVersionTag = null, bool disableTelemetry = false) { var connection = new PnPConnection(host, initializationType, url, clientContext, new Dictionary <TokenAudience, GenericToken>(1) { { tokenAudience, token } }, minimalHealthScore, pnpVersionTag, disableTelemetry) { ConnectionMethod = ConnectionMethod.AccessToken, Tenant = token.ParsedToken.Claims.FirstOrDefault(c => c.Type.Equals("tid", StringComparison.InvariantCultureIgnoreCase))?.Value, ClientId = token.ParsedToken.Claims.FirstOrDefault(c => c.Type.Equals("appid", StringComparison.InvariantCultureIgnoreCase))?.Value }; return(connection); }