Skip to content

Commit 2c70d2e

Browse files
author
Daniele Catanesi
committed
Added new post
1 parent acaadf4 commit 2c70d2e

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

_posts/2022-01-25-Cannot bind argument to parameter-Token-Expiry.md renamed to _posts/2022-01-25-Cannot bind argument to parameter-Token-Expiry - Copy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ header:
1818

1919
## Exchange Online Certificate Based authentication
2020

21-
Microsoft is, _finally_, disabling **basic authentication** (read username and password) in Exchange Online in favour of **Certificate Based authentication**.
21+
Microsoft is, _finally_, disabling **basic authentication** (read username and password) in Exchange Online in favor of **Certificate Based authentication**.
2222

2323
Once this change is fully implemented, around mid February at least for some tenants, connecting via username and passwords to Exchange Online will not be possible anymore.
2424

2525
You can read my article on how to implement _Certificate Based authentication_ for Exchange Online [here](https://pscustomobject.github.io/powershell/office365/exchange/Exchange-Online-Certificate-Based-Authentication/).
2626

27-
As a result of this change I started updating one of our automations, responsible for the whole lifecycle of our mailboxes, to ditch old credential objects in favour of the more secure Certificate Authentication.
27+
As a result of this change I started updating one of our automations, responsible for the whole life-cycle of our mailboxes, to ditch old credential objects in favor of the more secure Certificate Authentication.
2828

2929
This is when I encountered the _‌cannot bind argument to parameter 'token expiry time' because it is null._ error message.
3030

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: "Exchange Online Management Module - Could not use the certificate for signing"
3+
excerpt: "When trying to establish a connection to Exchange Online Could not use the certificate for signing error message is displayed."
4+
categories:
5+
- PowerShell
6+
- Exchange
7+
- Office 365
8+
9+
tags:
10+
- PowerShell
11+
- Office365
12+
- Exchange
13+
14+
toc: true
15+
header:
16+
teaser: "/assets/images/PowerShell_Logo.png"
17+
---
18+
19+
## Exchange Online Certificate Based authentication
20+
21+
As I have written in my [previous post about TokenExpiry error message Microsoft is retiring ability to connect to Exchange Online via basic authentication](https://pscustomobject.github.io/powershell/exchange/office%20365/Cannot-bind-argument-to-parameter-Token-Expiry/).
22+
23+
You can read my article on how to implement _Certificate Based authentication_ for Exchange Online [here](https://pscustomobject.github.io/powershell/office365/exchange/Exchange-Online-Certificate-Based-Authentication/).
24+
25+
## Could not use the certificate for signing error message
26+
27+
Today while I was updating code for one of our automations I created a request for a new certificate to use for authentication purposes.
28+
29+
Once I deployed code to our test environment automation was failing the connection to Exchange Online with the following error
30+
31+
```powershell
32+
[System.Management.Automation.RuntimeException] One or more errors occurred.
33+
[Microsoft.Identity.Client.MsalClientException] 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 aka.ms/msal-net-signed-assertion.
34+
[System.Security.Cryptography.CryptographicException] Invalid provider type specified.
35+
````
36+
37+
Funnily enough the same certificate and cmdlets were working fine with PowerShell 7.
38+
39+
After quite some troubleshooting I've found out the problem was caused by the certificate's private key using [*Cryptography Next Generate (CNG)*](https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/network/cng-certificates-overview) template rather than RSA.
40+
41+
Not having direct access to the CA releasing the certificate I could not change this so I had to resort on either running the automation in PowerShell 7 or update the certificate itself.
42+
43+
Luckily this is easily done via OpenSSL. Let's see how.
44+
45+
## Convert Certificate private key from CNG to RSA
46+
47+
If you have installed Git, cygwin or Windows Subsystem for Linux you just need to fire a bash prompt and use the following commands:
48+
49+
```bash
50+
# Extract the public key from the cert
51+
OpenSSL pkcs12 -in "CNGCertificate.pfx" -nokeys -out "temp.cer"
52+
53+
# Extract the private key
54+
OpenSSL pkcs12 -in "CNGCertificate.pfx" -nocerts -out "temp.pem"
55+
56+
# Convert key to RSA
57+
OpenSSL rsa -inform PEM -in "temp.pem" -out "temp.rsa"
58+
59+
# Finally create a new pfx file
60+
OpenSSL pkcs12 -export -in "temp.cer" -inkey "temp.rsa" -out "RSACertificate.pfx"
61+
````
62+
63+
**Note:** In the above commands I am not using a password for the certificate as everything is local to my machine but a password is definitely *required* when exporting a certificate together with the private key.
64+
{: .notice--warning}
65+
66+
Once the new pfx file has been created all *temporary* certificates can be safely removed form the system and connection to Exchange Online will go through just fine.
67+
68+
Again if you can use PowerShell 7 you will not face this issue but in case you're stuck with version 5.1 and facing this error message hopefully this post can save you some headaches.
69+
70+
Full credit for the solution goes to this [StackOverFlow thread](https://stackoverflow.com/questions/22581811/invalid-provider-type-specified-cryptographicexception-when-trying-to-load-pri/34103154#34103154)

0 commit comments

Comments
 (0)