Connect to Exchange Online using a Certificate

Connect to Exchange Online with Certificate

In today’s cloud-driven world, securing access to critical Microsoft 365 business resources like Exchange Online is crucial. While traditional username and password combinations have served us well, they’re increasingly vulnerable to phishing attacks and brute-force breaches. This is where Certificate based authentication (CBA) steps in, offering a robust and secure alternative to traditional username and password authentication for Exchange Online. Let’s get started with how to connect to Exchange Online with certificate!  

What is Certificate Based Authentication? 

To run scripts in Microsoft 365 for Exchange Online, you usually need to save usernames and passwords in files or secret vaults. Well, this method isn’t very secure. Certificate based authentication offers a safer way. It uses Azure AD apps and self-signed certificates for unattended Exchange Online PowerShell automation. 

How Certificate Based Authentication Works? 

Here’s how CBA functions: 

1️⃣ Token Retrieval: The Exchange Online PowerShell module uses the Active Directory Authentication Library (ADAL) to generate an app-only token. 
2️⃣ Credentials: This token is obtained using the application ID, tenant ID, and certificate thumbprint. 

  • Application ID: A unique identifier for the Azure AD application, which registers the app with Azure Active Directory. 
  • Tenant ID: A unique identifier for the Azure AD tenant, which specifies the organization’s directory. 
  • Certificate Thumbprint: A hash value used to identify a certificate, ensuring the application can securely authenticate without needing user credentials

3️⃣ Role Assignment: The Azure AD application is assigned a Directory Role, included in the token. 
4️⃣ Access Control: The role information in the token sets up the session’s permissions, ensuring the right access level for the task. 

This process ensures a more secure and efficient way to automate tasks in Microsoft 365. 

How to Connect to Exchange Online with Certificate? 

To set up certificate based authentication in Exchange Online, follow the steps mentioned below: 

  1. Create and export a self-signed certificate 
  2. Register the application in Microsoft Entra ID 
  3. Upload the certificate to the registered application 
  4. Assign API permissions to the application 
  5. Add role assignment to the application 
  6. Connect to Exchange Online using a certificate 

1. Create and Export a Self-Signed Certificate  

Microsoft Entra ID offers app-only authentication, allowing your applications to access resources without needing a user login. To achieve this, you’ll use a certificate instead of a password.  

Self-signed certificates (SSL) are ideal for testing and offer a secure option for administrators requiring certificate based authentications. By using the New-SelfSignedCertificate PowerShell cmdlet, you can effortlessly generate an SSL certificate tailored to your specific needs and requirements. 

Generate a Self-Signed Certificate using PowerShell 

To create a self-signed certificate using PowerShell, run the following command. 

$Certificate = New-SelfSignedCertificate -Subject <certificatename> -CertStoreLocation Cert:\CurrentUser\My  

Replace <certificatename> with the desired name for your certificate. 

Export the Self-Signed Certificate using PowerShell  

To export the certificate, run the Export-Certificate cmdlet. When you export a single certificate, the default file format is set to CERT.  

To export the certificate in .cer format, use the script below. 

 # Get the certificate by its subject name  
$Cert = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object {$_.Subject -eq "CN=<certificatename>"}  
 
# Export certificate to .cer file with password protection  
$CertCerPath = "<certificatepath>.cer"  
Export-Certificate -Cert $Cert -FilePath $CertCerPath -Type CERT  
$Password = ConvertTo-SecureString -String "YourPassword" -Force –AsPlainText  
 

Replace <certificatename> with the actual name of your certificate.  
Replace <certificatepath> with the path where you want to save the .cer file.  
Replace “YourPassword” with your desired password to protect the .cer file. 

For example, 

# Get the certificate by its subject name  
$Cert = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object {$_.Subject -eq "CN=EXOcertificate"}  
 
# Export certificate to .cer file with password protection  
$CertCerPath = " C:\Users\Harvey\Downloads\EXOcertificate.cer"  
Export-Certificate -Cert $Cert -FilePath $CertCerPath -Type CERT  
$Password = ConvertTo-SecureString -String "certificate@999" -Force –AsPlainText 

2. Register the Application in Microsoft Entra ID 

To enable app-only authentication for unattended scripts in Exchange Online, admins must register the application in Microsoft Entra ID as follows.  

  1. Sign in to the Microsoft Entra Portal
  2. Navigate to Applications –> App registrations –> New registration.  
  3. Enter a name for your application and choose the suitable account type. 
  4. Click Register
Connect to Exchange Online with Certificate
Register an application in Microsoft Entra

Once the application is registered, you will be redirected to its overview page. Note down the generated Application (client) ID, as it will be used in the last step. 

Connect to Exchange Online with Certificate
Get the App registration ID

3. Upload the Certificate to the Registered Application 

After getting the app registration ID, navigate to Certificates & secrets on the same page to upload the SSL certificate we generated earlier.  

  1. Switch to the Certificates tab. 
  2. Select Upload certificate
  3. Browse the .cert file and add it. 

Once you upload the certificate to the application, the certificate will now display as installed. Here, note down the Thumbprint, as it will be used to connect to EXO PowerShell in the last step. 

Upload the certificate to the registered application

4. Assign API Permission to the Application 

The next step is to configure permissions for the registered application. To do so, click on API Permissions under ‘Manage’. 

  1. Click on Add a permission
  2. Switch to the APIs my organization uses tab. 
  3. Search for Office 365 Exchange Online and select it. 
  4. Then, choose Application permissions
  5. From the available permissions, select Exchange.ManageAsApp from the ‘Exchange’ dropdown. 
  6. Select Add permissions
Assign API permissions to the application

To use the Exchange.Manage.AsApp permission, admin consent from your tenant is required. So, Click the “Grant admin consent for {{organization name}}” button, then confirm by clicking Yes at the top. 

5. Add Role Assignment to the Application 

Now, admins need to assign an Exchange administrator role to the application. To do so,   

  1. Navigate to Role and administrators under ‘Manage’. 
  2. Search for Exchange administrator and click on it. 
Assign Exchange administrator role to the application
  1. Choose Add assignments. 
  2. Under the ‘Membership’ tab, click to select members. 
  3. Now, search for the application’s name that we registered in the above steps and select it. 
  4. Click on Next
  5. Then, Enter Justification under the ‘Setting’ tab. 
  6. Finally, click on Assign.  
Role assignment to the application

That’s it! Now, you are good to connect to Exchange Online using a certificate. 

Note – If you feel Entra app registration tedious, you can check out the PowerShell script to automate Azure app registration!

6. Connect to Exchange Online using a Certificate 

To connect to Exchange Online PowerShell using certificate, the following details are needed. 

  • Tenant address of your Microsoft 365 organization. 
  • App ID and Certificate Thumbprint, which we obtained in the previous steps. 

For Certificate based authentication for Exchange Online PowerShell, use the following command. 

Connect-ExchangeOnline -CertificateThumbPrint <certifcationthumbprint> -AppID <appid> -Organization <tenantaddress> 

For example, 

Connect-ExchangeOnline -CertificateThumbPrint "022THISISADEMOTHUMBPRINT" -AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" -Organization "contoso.onmicrosoft.com" 

Once you have successfully connected to Exchange Online PowerShell using certificate, you can execute various commands to manage your Exchange Online environment such as Get-Mailbox, Get-MailboxFolderPermission, and others. 

Similarly, you can also connect to SharePoint Online using certificate. I hope this blog has given you all the information you need to connect to Exchange Online using a certificate. If you have any questions, feel free to reach out in the comments section. 

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect to Exchange Online with Certificate

time to read: 5 min
Follow us!