Sub topic 11
SAML single sign-on for connected apps
This chapter assumes you know how to create a connected app and set its policies, and that you can read a short XML document. It covers using a connected app to sign users in to a third-party application with SAML, instead of calling Salesforce APIs with OAuth.
Where SAML fits
OAuth lets an external app call Salesforce on a user's behalf. SAML (Security Assertion Markup Language) does something else. It lets a user sign in to an external app by proving who they are with a signed XML document called an assertion.
In this setup Salesforce is the identity provider (IdP), the system that knows the user. The external app is the service provider (SP), the system that trusts the IdP's assertion. A connected app with SAML enabled is Salesforce's record of one such service provider.
This is the reverse of the more common SAML setup, where Salesforce is the service provider and your corporate IdP signs users in to Salesforce. That setup uses Setup > Single Sign-On Settings and has nothing to do with connected apps.
A single connected app can have OAuth settings, SAML settings, or both. They are independent. A connected app with SAML only never issues a consumer key that is useful for API calls.
Prerequisites in the org
Before you enable SAML on a connected app, check three things:
- My Domain is deployed. The SAML endpoints and the issuer are based on your My Domain URL.
- Salesforce is enabled as an identity provider (Setup > Identity Provider). This step selects the signing certificate. Salesforce uses it to sign every assertion.
- You have the service provider's values: its Entity ID (a unique name for the SP) and its Assertion Consumer Service (ACS) URL (where the SP receives assertions).
Configuring the SAML settings
In the connected app, select Enable SAML under Web App Settings. The main fields are:
| Field | Purpose |
|---|---|
| Entity ID | Identifies the service provider. It must match what the SP expects in the assertion's audience. |
| ACS URL | The SP endpoint that receives the signed assertion by HTTP POST. |
| Subject Type | The value Salesforce places in the assertion to identify the user. |
| Name ID Format | The SAML format of that identifier, such as email address or persistent. |
| Issuer | Normally your My Domain URL. The SP checks it against its trusted IdP. |
| IdP Certificate | The certificate that signs assertions. The SP needs its public part. |
| Start URL | Where the user lands after an IdP-initiated login. |
| Verify Request Signatures | Requires SP-initiated requests to be signed. Upload the SP's certificate. |
| Encrypt SAML Response | Encrypts the assertion with the SP's certificate. |
Choosing the subject type
The subject type is the setting that most often causes login failures. The usual options are:
- Username. Simple, but usernames change when a user is renamed or when a sandbox is refreshed.
- Federation ID. A value stored on the user record, such as an employee ID. It does not change when the username does, so it is the usual choice for production.
- User ID. A stable Salesforce ID, but the SP has to store it.
- Persistent ID. An opaque identifier unique to the user and the SP.
Whatever you choose, the SP must have the same value stored for each person. If the SP matches users by email address, set the Name ID Format to email address and make the subject a field that carries that email.
The login flow
Users can start from Salesforce (the App Launcher tile, an IdP-initiated login) or from the SP (SP-initiated). The diagram shows the SP-initiated case.
In the IdP-initiated case, the user clicks the app in the App Launcher. Salesforce skips the first four steps and sends the signed response straight to the ACS URL.
What the assertion contains
A decoded assertion has the user's identity and conditions the SP must enforce. A trimmed example:
<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
ID="_a1b2c3" IssueInstant="2026-12-01T08:12:16.000Z" Version="2.0">
<saml:Issuer>https://MyDomain.my.salesforce.com</saml:Issuer>
<saml:Subject>
<saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">
[email protected]
</saml:NameID>
</saml:Subject>
<saml:Conditions NotBefore="2026-12-01T08:12:16.000Z"
NotOnOrAfter="2026-12-01T08:17:16.000Z">
<saml:AudienceRestriction>
<saml:Audience>https://app.example.com/saml/metadata</saml:Audience>
</saml:AudienceRestriction>
</saml:Conditions>
</saml:Assertion>The SP should reject an assertion if the signature is invalid, the issuer is unknown, the audience is not its own Entity ID, or the current time falls outside the NotBefore and NotOnOrAfter window.
To debug a failed login, capture the SAMLResponse form value from the browser's network tab. Decode it with this script:
import base64
import sys
import xml.dom.minidom
saml_response = sys.stdin.read().strip()
xml_bytes = base64.b64decode(saml_response)
print(xml.dom.minidom.parseString(xml_bytes).toprettyxml())Save the captured value to response.txt, then run python decode_saml.py < response.txt. If you enabled encryption, the assertion appears as an encrypted element and you need the SP's private key to read it.
Access control and policies
SAML does not bypass the connected app's access rules. Under Manage, the Permitted Users policy applies as it does for OAuth. With "Admin approved users are pre-authorized", a user must hold a profile or permission set assigned to the app, or Salesforce refuses to issue the assertion. The app's tile then does not appear in the App Launcher for that user.
Treat the assignment of profiles and permission sets as the access list for the SP. Removing a user from the permission set stops new logins from Salesforce. It does not end a session the SP has already created, because that session belongs to the SP. For quick offboarding, deactivate the user in Salesforce and also end their session in the SP.
External client apps can also carry SAML settings, and the settings-versus-policies split applies there as well. Check the current release notes for the exact fields available.
Common errors
| Symptom | Likely cause |
|---|---|
| SP reports "audience mismatch" | Entity ID in the connected app differs from the one the SP expects, including trailing slashes. |
| SP reports invalid signature | The SP holds an old IdP certificate, for example after a certificate rotation. |
| User not found at the SP | Subject type value does not match what the SP stored, such as username versus Federation ID. |
| Assertion rejected as expired | Clock skew between Salesforce and the SP, or the user waited too long on the login page. |
| Salesforce shows an insufficient privileges error | The user has no profile or permission set assigned to the app. |
Exercise
Users sign in to a third-party app through a SAML connected app. After a sandbox refresh, every login fails with "user not found" at the app, but the assertion is signed correctly and the audience matches. The subject type is Username. What is the likely cause, and what change makes the setup more stable?
Show answer
Sandbox refreshes append the sandbox name to usernames, so the NameID Salesforce sends no longer matches the usernames stored in the SP. Switch the subject type to Federation ID and populate that field with a stable value, such as an employee ID, on each user. Make sure the SP stores the same value.