{"id":7486,"date":"2020-08-26T04:30:51","date_gmt":"2020-08-26T04:30:51","guid":{"rendered":"http:\/\/blogs.aaddevsup.xyz\/?p=7486"},"modified":"2023-03-15T20:39:12","modified_gmt":"2023-03-15T20:39:12","slug":"using-powershell-to-configure-a-signing-certificate-for-a-saml-based-sso-enterprise-application","status":"publish","type":"post","link":"https:\/\/blogs.aaddevsup.xyz\/2020\/08\/using-powershell-to-configure-a-signing-certificate-for-a-saml-based-sso-enterprise-application\/","title":{"rendered":"Using PowerShell to configure a signing certificate for a SAML-based SSO Enterprise Application"},"content":{"rendered":"\n
In my last<\/a> blog post I talked about how to use PowerShell to instantiate an MSAL Confidential Client Application<\/a> to acquire an access token using Client Credentials Grant<\/a> flow. In this post we will use PowerShell to instantiate an MSAL Public Client Application<\/a> to perform an Authorization Code Grant<\/a> flow to obtain a delegated permission Access Token for Microsoft Graph. We will then use that access token to call Microsoft Graph to configure a signing certificate for our SAML Application Service Principal. Just a quick refresher that a certificate is always required when setting up SAML Sigle Sign-On feature for an Enterprise App in Azure AD.<\/p>\n\n\n\n To run the script in this blog you should have the following:<\/p>\n\n\n\n 1) A SAML-based SSO Enterprise Application you want to configure a signing certificate for. Get the Object ID of this Application in the Properties blade of the Enterprise App<\/strong> – we will need it for the script. The script in this blog performs the same thing as doing the following UI action in the portal:<\/p>\n\n\n\n Azure Active Directory -> Enterprise Application -> Pick the correct App -> Single sign-on -> click ‘Edit’ link in the SAML Signing Certificate section -> Import Certificate<\/p>\n\n\n\n If you are trying to automate a SAML-based SSO Application, take a look at the documentation Automate SAML-based SSO app configuration with Microsoft Graph API<\/a>. This blog can help with step 4, Configure Signing Certifcate, of that article.<\/p>\n\n\n\n 2) An app registration to sign in a user and get an access token for Microsoft Graph. Get the Application (client) ID of this app in the Overview section<\/strong> – we will need it for the script. This application should have the following App Registration configuration:<\/p>\n\n\n\n 3) The user who logs in to get the MS Graph Access Token should be one of the following Azure AD Administrative Role – this is required in order to make a change to the Service Principal:<\/p>\n\n\n\n We will need to have a certificate to configure for our application. You can either create a self-signed certificate (using either PowerShell or OpenSSL as shown below) or obtain one from your Trusted Certificate Authority. We will need the following certificate components for our script:<\/p>\n\n\n\n Note<\/strong>: It is important to have the private key in PKCS#12 format since Azure AD does not support other format types. Using the wrong format can result in the the error “Invalid certificate: Key value is invalid certificate” when using MS Graph to PATCH the Service Principal with a keyCredentials containing the certificate info<\/p>\n\n\n\n <\/p>\n\n\n\n The following PowerShell script can be used to create a self-signed certificate and then export both the private key and public key out to a .pfx and and a .cer files<\/p>\n\n\n\n If you don’t have OpenSSL installed already, refer to the OpenSSL documentation<\/a> for building and installation instruction. For Windows users, this StackOverflow discussion<\/a> has some useful information on how to download OpenSSL for Windows.<\/p>\n\n\n\n 3) convert the crt file to DER encoded binary X.509 .cer file:<\/p>\n\n\n\n On Windows, double-click on the crt file to launch the certificate wizard. Go to ‘Details’ tab and click on ‘Copy to File…’ button:<\/p>\n\n\n\nPre-requisites<\/h3>\n\n\n\n
\n\n\n\nSupported account types<\/td> Accounts in this organizational directory only<\/td><\/tr> Redirect URIs<\/td> http:\/\/localhost under ‘Mobile and desktop applications’ platform<\/td><\/tr> API permissions<\/td> Microsoft Graph – Delegated permissions: Application.ReadWrite.All<\/strong> and User.Read<\/strong>
(Make sure you grant Admin consent to these permissions)
<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n\n
Signing Certificate<\/h3>\n\n\n\n
\n
Using PowerShell to create a self-signed certificate<\/h4>\n\n\n\n
# fqdn - this is used for the 'issued to' and 'issued by' field of the certificate\n# pwd - password for exporting the certificate private key\n# location - path to folder where both the pfx and cer file will be written to, for example C:\\users\\john\\Documents\n\nParam(\n [Parameter(Mandatory=$true)]\n [string]$fqdn,\n [Parameter(Mandatory=$true)]\n [string]$pwd,\n [Parameter(Mandatory=$true)]\n [string]$location\n) \n\nif (!$PSBoundParameters.ContainsKey('location'))\n{\n $location = \".\"\n} \n\n$cert = New-SelfSignedCertificate -certstorelocation cert:\\currentuser\\my -DnsName $fqdn\n$pwdSecure = ConvertTo-SecureString -String $pwd -Force -AsPlainText\n$path = 'cert:\\currentuser\\my\\' + $cert.Thumbprint\n$cerFile = $location + \"\\\\\" + $fqdn + \".cer\"\n$pfxFile = $location + \"\\\\\" + $fqdn + \".pfx\" \n\nExport-PfxCertificate -cert $path -FilePath $pfxFile -Password $pwdSecure\nExport-Certificate -cert $path -FilePath $cerFile<\/pre>\n\n\n\n
Using OpenSSL to create a self-signed certificate<\/h4>\n\n\n\n
1) Run the following openssl command to create a public key file (crt) and private key file (pem) with your info<\/span>. See https:\/\/www.digicert.com\/kb\/ssl-support\/openssl-quick-reference-guide.htm for some openssl reference guide.\n\nopenssl<\/strong> req-x509 -sha256 -days 365 -newkey rsa:2048 -keyout \"C:\\Users\\path\\privateKey.key<\/span>\" -out \"C:\\Users\\path\\certificate.crt<\/span>\" -subj '\/C=your country\/ST=your state\/L=your locality\/O=Your Company, Inc.\/OU=your Organizational Unit\/CN=yourdomain.com<\/span>'\n\n2) convert the pem file to pfx file with your info<\/span>:\n\nopenssl<\/strong> pkcs12 -export -out \"C:\\Users\\path\\certificate.pfx<\/span>\" -inkey \"C:\\Users\\path\\privateKey.key<\/span>\" -in \"C:\\Users\\path\\certificate.crt<\/span>\" -passout pass:your password<\/span> -passin pass:your password<\/span><\/pre>\n\n\n\n