SOLVED: Connect-ExchangeOnline: Could not use the certificate for signing. See inner exception for details. Possible cause: this may be a known issue with apps build against .NET Desktop 4.6 or lower.

When you try and connect to Exchange Online using a service principal and client certificate such as this

Connect-ExchangeOnline -AppId "identifier" -CertificateThumbprint "thumbprint" -Organization "yourdomain.onmicrosoft.com";

You may see the following error

Could not use the certificate for signing. See inner exception for details. Possible cause: this may be a known issue with apps build against .NET Desktop 4.6 or lower. Either target a higher version of .NET desktop - 4.6.1 and above, or use a different certificate type (non-CNG) or sign your own assertion as described at https://aka.ms/msal-net-signed-assertion. 

Problem: The certificate you're using is too secure for the Exchange Online PowerShell cmdlets.

If you read the Microsoft Guidance here you'll see their examples use older encryption which does work correctly.

https://learn.microsoft.com/powershell/exchange/app-only-auth-powershell-v2?view=exchange-ps#step-3-generate-a-self-signed-certificate

This seems to be an issue with the Exchange Online PowerShell (even at version 3.4.0) perhaps the libraries are compiled using an old version of .NET?


Solution:

It's not a great solution you'll need to access the Microsoft Authentication libraries (MSAL)
https://www.nuget.org/packages/Microsoft.Identity.Client/

You can then authenticate with the Microsoft authentication servers and get the token to allow access to Exchange that you need yourself. This token can then be passed to the Connect-ExchangeOnline cmdlet.

# Set the variables
$msalPath = "D:\CENTRELSolutions\3rdParty\Microsoft\Microsoft Authentication Library (MSAL)";
$thumbprint = "yourthumbprint";
$applicationIdentifier = "appidentifier";
$organization = "yourorganization.onmicrosoft.com";
$environment = [Microsoft.Identity.Client.AzureCloudInstance]::AzurePublic;


# Authenticate
Add-Type -Path "$msalPath\Microsoft.IdentityModel.Abstractions.dll";
Add-Type -Path "$msalPath\Microsoft.Identity.Client.dll";
$certificate = Get-ChildItem -Path "Cert:\CurrentUser\My\$thumbprint";
[string[]] $Scopes = "https://outlook.office365.com/.default";
[Microsoft.Identity.Client.IConfidentialClientApplication] $application =
[Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($applicationIdentifier).WithCertificate($certificate).WithAuthority($environment, $tenantIdentifier).Build();
$result = $application.AcquireTokenForClient($scopes).ExecuteAsync().Result;

# You can now connect to Exchange Online
Connect-ExchangeOnline -AccessToken $result.AccessToken -Organization $organization;




Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations