Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2755 from KoenZomers/FixDisconnectIgnoringConnect…
Browse files Browse the repository at this point in the history
…ionAttribute

Fixed issue with Disconnect-PnPOnline not cleaning up certificate
  • Loading branch information
wobba authored Jun 24, 2020
2 parents d8bb581 + e141c19 commit f633253
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Added

### Changed
- Fixed issue where using `Disconnect-PnPOnline -Connection $variable` after having connected using `$variable = Connect-PnPOnline -CertificatePath <path> -ReturnConnection`, it would not clean up the certificate on that connection instance passed in by $variable, but instead try to do it on the current connection context [PR #2755](https://github.com/pnp/PnP-PowerShell/pull/2755)

### Contributors
- Gautam Sheth [gautamdsheth]
- Koen Zomers [koenzomers]

## [3.22.2006.2]

Expand Down
16 changes: 11 additions & 5 deletions Commands/Base/DisconnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace SharePointPnP.PowerShell.Commands.Base
{
[Cmdlet(VerbsCommunications.Disconnect, "PnPOnline")]
[CmdletHelp("Disconnects the context",
"Disconnects the current context and requires you to build up a new connection in order to use the Cmdlets again. Using Connect-PnPOnline to connect to a different site has the same effect.",
DetailedDescription = "Disconnects the current context and requires you to build up a new connection in order to use the Cmdlets again. Using Connect-PnPOnline to connect to a different site has the same effect.",
SupportedPlatform = CmdletSupportedPlatform.All,
Category = CmdletHelpCategory.Base)]
[CmdletExample(
Code = @"PS:> Disconnect-PnPOnline",
Expand All @@ -22,16 +23,21 @@ public class DisconnectOnline : PSCmdlet

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(PnPConnection.CurrentConnection?.Certificate != null)
if(Connection?.Certificate != null)
{
#if !NETSTANDARD2_1
PnPConnectionHelper.CleanupCryptoMachineKey(PnPConnection.CurrentConnection.Certificate);
PnPConnectionHelper.CleanupCryptoMachineKey(Connection.Certificate);
#endif
PnPConnection.CurrentConnection.Certificate = null;
Connection.Certificate = null;
}
#endif
var success = false;
var success = false;
if (Connection != null)
{
success = DisconnectProvidedService(Connection);
Expand Down
18 changes: 15 additions & 3 deletions Commands/Base/PnPConnectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,28 @@ internal static PnPConnection InitiateAzureAdAppOnlyConnectionWithClientIdClient
return spoConnection;
}

/// <summary>
/// Verifies if a local copy of the certificate has been stored in the machinekeys cache of Windows and if so, will remove it to avoid the cache from growing over time
/// </summary>
/// <param name="certificate">Certificate to validate if there is a local cached copy for and if so, delete it</param>
internal static void CleanupCryptoMachineKey(X509Certificate2 certificate)
{
var privateKey = (RSACryptoServiceProvider)certificate.PrivateKey;
string uniqueKeyContainerName = privateKey.CspKeyContainerInfo.UniqueKeyContainerName;
certificate.Reset();
var path = Environment.GetEnvironmentVariable("ProgramData");
if (string.IsNullOrEmpty(path)) path = @"C:\ProgramData";

var programDataPath = Environment.GetEnvironmentVariable("ProgramData");
if (string.IsNullOrEmpty(programDataPath))
{
programDataPath = @"C:\ProgramData";
}
try
{
System.IO.File.Delete(string.Format(@"{0}\Microsoft\Crypto\RSA\MachineKeys\{1}", path, uniqueKeyContainerName));
var temporaryCertificateFilePath = $@"{programDataPath}\Microsoft\Crypto\RSA\MachineKeys\{uniqueKeyContainerName}";
if (System.IO.File.Exists(temporaryCertificateFilePath))
{
System.IO.File.Delete(temporaryCertificateFilePath);
}
}
catch (Exception)
{
Expand Down

0 comments on commit f633253

Please sign in to comment.