<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Connect to Exchange Online Certificate Based Authentication Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/connect-to-exchange-online-certificate-based-authentication/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/connect-to-exchange-online-certificate-based-authentication/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Sun, 06 Nov 2022 20:09:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Exchange Online Certificate Based Authentication</title>
		<link>https://thesysadminchannel.com/exchange-online-certificate-based-authentication/</link>
					<comments>https://thesysadminchannel.com/exchange-online-certificate-based-authentication/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Sun, 06 Nov 2022 19:38:40 +0000</pubDate>
				<category><![CDATA[Exchange Online]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[app-only authentication exchange online]]></category>
		<category><![CDATA[azure app registration certificate authentication]]></category>
		<category><![CDATA[certificate-based authentication for exchange online remote powershell]]></category>
		<category><![CDATA[Connect to Exchange Online Certificate Based Authentication]]></category>
		<category><![CDATA[connect-exchange online certificate thumbprint]]></category>
		<category><![CDATA[exchange certificate based authentication]]></category>
		<category><![CDATA[Exchange Online certificate-based authentication]]></category>
		<category><![CDATA[office 365 certificate-based authentication]]></category>
		<category><![CDATA[remote powershell using certificate-based authentication]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=4477</guid>

					<description><![CDATA[<p>As a Systems Engineer I am constantly looking for ways to improve processes as well as look for ways to automate everything I possibly can. As a general rule of thumb, I try to automate myself out of a job&#8230; <a href="https://thesysadminchannel.com/exchange-online-certificate-based-authentication/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/exchange-online-certificate-based-authentication/">Exchange Online Certificate Based Authentication</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>As a Systems Engineer I am constantly looking for ways to improve processes as well as look for ways to automate everything I possibly can.  As a general rule of thumb, I try to automate myself out of a job so everything can run silky smooth should I ever get hit by a bus.  Since I work primary in Microsoft 365 and Azure AD, I thought it would be great to share what I&#8217;ve learned in order to use that automation for Exchange Online. With that said, this article is going to be geared around <strong>Exchange Online Certificate Based Authentication</strong> and the steps to go 100% Passwordless.</p>
<div id="tableofcontents">
<h2>Table Of Contents</h2>
<ul>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#createcertificate">Create a Self-Signed Certificate</a></li>
<li><a href="#appregistration">Create an Azure App Registration and Service Principal</a></li>
<li><a href="#addexchangerole">Add Exchange Administrator Role</a></li>
<li><a href="#connecttoapp">Connect to Exchange Online using the Azure Application</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
</div>
<div id="requirements" style="scroll-margin-top: 15px;"></div>
<h2>Requirements</h2>
<p>In order to set this up without failure, there are a few things needed to get you on your way to using Exchange Online certificate based authentication.  Let&#8217;s cover what&#8217;s needed right now.<br />
&nbsp;</p>
<ul>
<li>A certificate, either self signed or one issued by PKI</li>
<li>Azure Application Administrator or Global Administrator</li>
<li>Privilege Role Administrator or Global Administrator</li>
<li>Exchange Online Management PowerShell module</li>
</ul>
<p>&nbsp;</p>
<p>Above are the requirements to allow you to connect to Exchange Online using certificates.  I manage Exchange Online using PowerShell so I added that as well.  If you&#8217;re looking for instructions on how to get that installed, check out this article to <a href="https://thesysadminchannel.com/how-to-install-exchange-online-powershell-module/" rel="noopener" target="_blank">install the Exchange Online Management module for PowerShell</a>.</p>
<div id="createcertificate" style="scroll-margin-top: 15px;"></div>
<h2>Create a Self-Signed Certificate</h2>
<p>First things first, I thought it would be best to start off by creating the self-signed certificate to get the ball rolling.  If possible, I would recommend using a certificate issued by a public key infrastructure (PKI). The reason for that is because we know we can trust it, it is inherently more secure, and we can also revoke the cert should the situation call for it. The problem is not every environment has a PKI setup (my lab included).<br />
&nbsp;</p>
<p>As mentioned, we don&#8217;t have a PKI in our environment so we&#8217;ll make due with a self signed certificate. Luckily, Azure does support self signed certs so let&#8217;s get that created within PowerShell.<br />
With PowerShell open, enter in the following:<br />
&nbsp;</p>
<pre class="brush: powershell; title: ; notranslate">
#splatting for human readability
$CertParam = @{
    'KeyAlgorithm'      = 'RSA'
    'KeyLength'         = 2048
    'KeyExportPolicy'   = 'NonExportable'
    'DnsName'           = 'server.thesysadminchannel.com'
    'FriendlyName'      = 'Exchange Online Automation App'
    'CertStoreLocation' = 'Cert:\CurrentUser\My\'
    'NotAfter'          = (Get-Date).AddYears(1)
}
 
#Creating self signed cert with parameters from above.
$Cert = New-SelfSignedCertificate @CertParam
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Self-Signed-Certificate.png" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Self-Signed-Certificate.png" alt="Self Signed Certificate" width="960" height="468" class="aligncenter size-full wp-image-4481" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Self-Signed-Certificate.png?v=1667715233 960w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Self-Signed-Certificate-768x374.png?v=1667715233 768w" sizes="(max-width: 960px) 100vw, 960px" /></a><br />
&nbsp;</p>
<p>The above parameters do not allow you to export the certificate to another machine.  I should also note that this is saving the certificate under the user context.  If you want to store the certificate under the local machine context, you will need to run PowerShell as an administrator anytime you to connect.  Allowing it under the local machine certificate store means other administrators on the machine would also be able to connect.  So just be aware.<br />
&nbsp;</p>
<p>Now that we have the cert created, let&#8217;s export it so we can upload it to Azure when we create our application.</p>
<pre class="brush: powershell; title: ; notranslate">
#Since we captured the output to the $Cert variable in our previous step.
#We will use that to specify the cert parameter. 
#The .cer file will exported to the user's desktop.
 
Export-Certificate -Cert $Cert -FilePath $Home\Desktop\ExchangeOnlineAutomation.cer
</pre>
<div id="appregistration" style="scroll-margin-top: 15px;"></div>
<h2>Create an Azure App Registration and Service Principal</h2>
<p>To get started, we need to make sure we have the proper rights to get the application created.  This is where you will need an Azure AD Application administrator (or Global administrator).<br />
&nbsp;</p>
<p>Within Azure AD:</p>
<ul>
<li>Navigate to <strong>App registrations</strong> → <strong>New registration</strong></li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/New-App-Registration.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/New-App-Registration.png" alt="New App Registration" width="876" height="395" class="aligncenter size-full wp-image-4478" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/New-App-Registration.png?v=1667705064 876w, https://thesysadminchannel.com/wp-content/uploads/2022/11/New-App-Registration-768x346.png?v=1667705064 768w" sizes="(max-width: 876px) 100vw, 876px" /></a><br />
&nbsp;</p>
<ul>
<li>Name your application accordingly.  I&#8217;ve named mine <strong>Exchange Online Automation</strong></li>
<li>Select Accounts in this organizational directory only (Single tenant)</li>
<li>Leave the Redirect URI empty</li>
<li>Click Register to create the app</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Register-new-app.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Register-new-app.png" alt="Register new app" width="1238" height="808" class="aligncenter size-full wp-image-4480" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Register-new-app.png?v=1667705806 1238w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Register-new-app-1024x668.png?v=1667705806 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Register-new-app-768x501.png?v=1667705806 768w" sizes="(max-width: 1238px) 100vw, 1238px" /></a><br />
&nbsp;</p>
<p>With your app now created:</p>
<ul>
<li>Navigate to Certificates &#038; secrets</li>
<li>Click the certificates tab</li>
<li>Click Upload certificate</li>
<li>Click the folder icon and browse to your desktop to select the exported cert</li>
<li>Click Add</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Upload-Certificate-to-Azure-App.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Upload-Certificate-to-Azure-App.png" alt="Upload Certificate to Azure App" width="1475" height="833" class="aligncenter size-full wp-image-4484" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Upload-Certificate-to-Azure-App.png?v=1667717494 1475w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Upload-Certificate-to-Azure-App-1024x578.png?v=1667717494 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Upload-Certificate-to-Azure-App-768x434.png?v=1667717494 768w" sizes="(max-width: 1475px) 100vw, 1475px" /></a></p>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Certificate-Setting-for-Azure-App.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Certificate-Setting-for-Azure-App.png" alt="Certificate Setting for Azure App" width="854" height="238" class="aligncenter size-full wp-image-4485" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Certificate-Setting-for-Azure-App.png?v=1667717853 854w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Certificate-Setting-for-Azure-App-768x214.png?v=1667717853 768w" sizes="(max-width: 854px) 100vw, 854px" /></a><br />
&nbsp;</p>
<p>Next we need to add the <code>Exchange.ManageAsApp</code> API permissions within the app so the application object can access the resource.  To do this we need to add it through the manifest because we won&#8217;t be able to find it via the typical API permissions blade.<br />
&nbsp;</p>
<p>Within the app, navigate to the manifest blade and replace the <code>requiredResourceAccess</code> block with this code. Be sure to click save when it&#8217;s added.</p>
<pre class="brush: powershell; title: ; notranslate">
&quot;requiredResourceAccess&quot;: [
   {
      &quot;resourceAppId&quot;: &quot;00000002-0000-0ff1-ce00-000000000000&quot;,
      &quot;resourceAccess&quot;: [
         {
            &quot;id&quot;: &quot;dc50a0fb-09a3-484d-be87-e023b12c6440&quot;,
            &quot;type&quot;: &quot;Role&quot;
         }
      ]
   }
],
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/App-role-via-App-manifest.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/App-role-via-App-manifest.png" alt="App role via App manifest" width="1473" height="822" class="aligncenter size-full wp-image-4489" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/App-role-via-App-manifest.png?v=1667753748 1473w, https://thesysadminchannel.com/wp-content/uploads/2022/11/App-role-via-App-manifest-1024x571.png?v=1667753748 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/App-role-via-App-manifest-768x429.png?v=1667753748 768w" sizes="(max-width: 1473px) 100vw, 1473px" /></a><br />
&nbsp;</p>
<p>Once that is saved, we can verify it was added correctly by going back to API permissions.  We will now see that Exchange.ManageAsApp is the only entry there.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Admin-consent-to-Exchange-ManageasApp.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Admin-consent-to-Exchange-ManageasApp.png" alt="Admin consent to Exchange ManageasApp" width="1469" height="650" class="aligncenter size-full wp-image-4492" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Admin-consent-to-Exchange-ManageasApp.png?v=1667754688 1469w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Admin-consent-to-Exchange-ManageasApp-1024x453.png?v=1667754688 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Admin-consent-to-Exchange-ManageasApp-768x340.png?v=1667754688 768w" sizes="(max-width: 1469px) 100vw, 1469px" /></a><br />
&nbsp;</p>
<p>However, we will notice that the app requires admin consent in order for it to be effective.  Go ahead and consent to it now.  Once complete, it should look like the image below.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/admin-consent-has-been-granted.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/admin-consent-has-been-granted.png" alt="admin consent has been granted" width="1081" height="195" class="aligncenter size-full wp-image-4493" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/admin-consent-has-been-granted.png?v=1667754902 1081w, https://thesysadminchannel.com/wp-content/uploads/2022/11/admin-consent-has-been-granted-1024x185.png?v=1667754902 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/admin-consent-has-been-granted-768x139.png?v=1667754902 768w" sizes="(max-width: 1081px) 100vw, 1081px" /></a></p>
<div id="addexchangerole" style="scroll-margin-top: 15px;"></div>
<h2>Add Exchange Administrator Role</h2>
<p>With our app now created and configured properly, we&#8217;ll need to grant the Exchange Administrator role to that app.<br />
&nbsp;</p>
<p>Within Azure AD:</p>
<ul>
<li>Navigate to Roles and administrators</li>
<li>Search for Exchange and click on Exchange administrator</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-Admin-role.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-Admin-role.png" alt="Exchange Admin role" width="1459" height="664" class="aligncenter size-full wp-image-4494" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-Admin-role.png?v=1667755387 1459w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-Admin-role-1024x466.png?v=1667755387 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-Admin-role-768x350.png?v=1667755387 768w" sizes="(max-width: 1459px) 100vw, 1459px" /></a><br />
&nbsp;</p>
<ul>
<li>You should be taken to the <strong>active assignments</strong> for the Exchange admin role</li>
<li>Click on <strong>Add assignments</strong></li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/add-assignments.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/add-assignments.png" alt="add assignments Azure AD role" width="860" height="284" class="aligncenter size-full wp-image-4495" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/add-assignments.png?v=1667755615 860w, https://thesysadminchannel.com/wp-content/uploads/2022/11/add-assignments-768x254.png?v=1667755615 768w" sizes="(max-width: 860px) 100vw, 860px" /></a><br />
&nbsp;</p>
<ul>
<li>Click <strong>no members selected</strong> link</li>
<li>Search for the app name (Our is <strong>Exchange Online Automation</strong>)</li>
<li>Click on the app to add it to the selection</li>
<li>Click select</li>
<li>Complete the prompts to add the role</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Add-Exchange-Role-to-Azure-App-1.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Add-Exchange-Role-to-Azure-App-1.png" alt="Add Exchange Role to Azure App-1" width="1457" height="803" class="aligncenter size-full wp-image-4500" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Add-Exchange-Role-to-Azure-App-1.png?v=1667759998 1457w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Add-Exchange-Role-to-Azure-App-1-1024x564.png?v=1667759998 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Add-Exchange-Role-to-Azure-App-1-768x423.png?v=1667759998 768w" sizes="(max-width: 1457px) 100vw, 1457px" /></a><br />
&nbsp;</p>
<p>We should now see our Service Principal listed as an active assignment.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-App-added-as-an-active-assignment.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-App-added-as-an-active-assignment.png" alt="Exchange App added as an active assignment" width="959" height="326" class="aligncenter size-full wp-image-4497" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-App-added-as-an-active-assignment.png?v=1667756322 959w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Exchange-App-added-as-an-active-assignment-768x261.png?v=1667756322 768w" sizes="(max-width: 959px) 100vw, 959px" /></a></p>
<div id="blockquote1">
<strong>Note</strong>: I chose to add this as an active assignment with application permissions because this is intended to be used for unattended automation.
</div>
<div id="connecttoapp" style="scroll-margin-top: 15px;"></div>
<h2>Connect to Exchange Online using the Azure Application</h2>
<p>Finally, we&#8217;re in a spot where we can put all of the pieces together and connect to Exchange Online using our Azure AD application (Service Principal).  Again, since I use PowerShell to manage EXO, we&#8217;re going to connect using the Exchange Online Management module.  Be sure to use the latest version.<br />
&nbsp;</p>
<p>Before we connect, let&#8217;s get the AppId.  We&#8217;ll also need to know the tenant&#8217;s default onmicrosoft name.  To get the AppId, go back to the overview page of the Application we created earlier.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/Get-AppId-for-the-app.jpg" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/Get-AppId-for-the-app.jpg" alt="Get AppId for the app" width="844" height="396" class="aligncenter size-full wp-image-4498" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/Get-AppId-for-the-app.jpg?v=1667757870 844w, https://thesysadminchannel.com/wp-content/uploads/2022/11/Get-AppId-for-the-app-768x360.jpg?v=1667757870 768w" sizes="(max-width: 844px) 100vw, 844px" /></a><br />
&nbsp;</p>
<pre class="brush: powershell; title: ; notranslate">
$AppId = '9e46ef5x-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$Certificate = Get-ChildItem Cert:\CurrentUser\My\A94FFE108DCxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$TenantName = 'thesysadminchannel.onmicrosoft.com'

Connect-ExchangeOnline -AppId $AppId -Certificate $Certificate -Organization $TenantName -ShowBanner: $false
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/11/exchange-online-certificate-based-authentication.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/11/exchange-online-certificate-based-authentication.png" alt="exchange online certificate based authentication" width="1146" height="457" class="aligncenter size-full wp-image-4499" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/11/exchange-online-certificate-based-authentication.png?v=1667759790 1146w, https://thesysadminchannel.com/wp-content/uploads/2022/11/exchange-online-certificate-based-authentication-1024x408.png?v=1667759790 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/11/exchange-online-certificate-based-authentication-768x306.png?v=1667759790 768w" sizes="(max-width: 1146px) 100vw, 1146px" /></a><br />
&nbsp;</p>
<p>As you can see, we were able to successfully connect to Exchange Online and run the Get-Mailbox command against my account.  As a side note, I&#8217;ve also chosen to not display the banner by using the <code>ShowBanner: $false</code> parameter in the command.</p>
<div id="conclusion" style="scroll-margin-top: 10px;"></div>
<h2>Conclusion</h2>
<p>Hopefully this article on how to use Exchange Online certificate based authentication was insightful and you were able to implement it in your own organization.  This is used pretty much daily to automate tasks in Exchange and it&#8217;s great that we don&#8217;t have to worry about usernames and passwords.<br />
&nbsp;</p>
<p>If you want more information on creating Azure apps and using Graph API, check out my in-depth article on <a href="https://thesysadminchannel.com/how-to-connect-to-microsoft-graph-api-using-powershell/" rel="noopener" target="_blank">how to Connect To Microsoft Graph API Using PowerShell</a>.</p>
<p>The post <a href="https://thesysadminchannel.com/exchange-online-certificate-based-authentication/">Exchange Online Certificate Based Authentication</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/exchange-online-certificate-based-authentication/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4477</post-id>	</item>
	</channel>
</rss>
