示例#1
0
        private static ClientContext CreateContext(string contextUrl, ICredentials credentials)
        {
            ClientContext context = null;

            if (!String.IsNullOrEmpty(AppId) && !String.IsNullOrEmpty(AppSecret))
            {
                OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();

                if (new Uri(contextUrl).DnsSafeHost.Contains("spoppe.com"))
                {
                    context = am.GetAppOnlyAuthenticatedContext(contextUrl, PnPConnectionHelper.GetRealmFromTargetUrl(new Uri(contextUrl)), AppId, AppSecret, acsHostUrl: "windows-ppe.net", globalEndPointPrefix: "login");
                }
                else
                {
                    context = am.GetAppOnlyAuthenticatedContext(contextUrl, AppId, AppSecret);
                }
            }
            else
            {
                context             = new ClientContext(contextUrl);
                context.Credentials = Credentials;
            }

            context.RequestTimeout = Timeout.Infinite;
            return(context);
        }
        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");
                }
            }
        }