When a Microsoft Entra server login is deleted from the logical master database in Azure SQL Database, any login-based database users mapped to that login become orphaned. These orphaned principals silently fail authentication with Error 18456 (internal state 5). This issue can remain hidden for months or years if an Entra admin account masks the broken group path. This article walks through identifying, diagnosing, and resolving this scenario.
Azure SQL Database supports two models for creating Microsoft Entra principals in a database: contained database users and login-based users. Both models work, but they have fundamentally different dependency chains. Understanding the difference is critical because choosing the wrong model, or breaking its dependencies, can produce authentication failures that are difficult to diagnose and easy to overlook.
This article documents a real-world scenario where Microsoft Entra group principals were created as login-based database users. When the corresponding server logins were later removed from the logical master database, the database users became orphaned. The resulting Error 18456 went undetected because the primary administrator was also the Entra admin on the server, a role that bypasses group-based principal resolution entirely.
We will cover the symptoms, investigation methodology, root cause, resolution, and preventive best practices.
Architecture:
- Azure SQL Database (single database, General Purpose tier)
- Multiple logical servers, each hosting several user databases
- Microsoft Entra ID authentication enabled
- Entra security groups used for role-based database access (e.g., "High Risk", "Low Risk" developer groups)
- One user configured as both an Entra group member and the Entra admin on the server
Principal setup across databases:
Some databases had Entra group principals created as contained database users (the recommended approach), while others had the same groups created as login-based database users. This inconsistency was introduced by different provisioning scripts or tooling used at different times.
The two principal models produce different SID (Security Identifier) formats in sys.database_principals:
| Creation Method | SID Length | SID Format | Server Login Required? |
|---|---|---|---|
| CREATE USER [X] FROM EXTERNAL PROVIDER | 16 bytes | Raw Entra object ID | No |
| CREATE USER [X] FROM LOGIN [X] | 18 bytes | Entra object ID + AADE suffix | Yes |
Client-side error (SSMS):
Login failed for user '<user_upn>'. (Microsoft SQL Server, Error: 18456)
Observed behavior:
- Users who are members of a Microsoft Entra security group receive Error 18456 when connecting to specific databases
- The same users can connect to other databases on the same or different logical servers without issues
- If the user is temporarily assigned as the Entra admin on the server, they can connect successfully to all databases
- The issue affects all members of the group, not just one user
- Clearing the browser token cache or re-authenticating does not resolve the issue
Key distinguishing characteristic:
The Entra admin can connect (mapping to dbo), but the same user cannot connect through group membership alone after the admin role is removed.
Investigation and Troubleshooting
Step 1: Identify the principal model in the affected database
Connect to the affected user database as the Entra admin and run:
SELECT name, type_desc, DATALENGTH(sid) AS sid_length, sys.fn_varbintohexstr(sid) AS sid_hex, CASE WHEN DATALENGTH(sid) = 16 THEN CONVERT(uniqueidentifier, sid) WHEN DATALENGTH(sid) = 18 THEN CONVERT(uniqueidentifier, SUBSTRING(sid, 1, 16)) ELSE NULL END AS entra_object_id, CASE WHEN DATALENGTH(sid) = 18 AND RIGHT(CONVERT(varchar(170), sid, 2), 4) = 'AADE' THEN 'LOGIN-BASED' ELSE 'CONTAINED' END AS mapping_model FROM sys.database_principals WHERE type IN ('E', 'X');
What to look for: Any principal showing LOGIN-BASED with an 18-byte SID is a candidate for this issue.
Step 2: Check for the corresponding server login
Connect to the logical master database and run:
SELECT name, type_desc, DATALENGTH(sid) AS sid_length, sys.fn_varbintohexstr(sid) AS sid_hex, CASE WHEN DATALENGTH(sid) = 16 THEN CONVERT(uniqueidentifier, sid) ELSE NULL END AS entra_object_id FROM sys.server_principals WHERE type IN ('E', 'X');
What to look for: If a login-based database user exists but no matching server principal appears in this query (same name or same first 16 SID bytes), the database user is orphaned.
Step 3: Verify the session identity
Run this in the affected database to confirm how the current connection resolves:
SELECT @@SERVERNAME AS connected_server, DB_NAME() AS connected_database, ORIGINAL_LOGIN() AS original_login, SUSER_SNAME() AS server_identity, USER_NAME() AS database_identity;
If database_identity returns dbo, the current session is using the Entra admin path, not the group path. Any IS_MEMBER() calls from this session will return 0 for Entra groups because the dbo identity does not resolve group membership. This is expected behavior, not evidence that the user is not a group member.
Step 4: Check the user's token and group membership
SELECT principal_id, sid, name, type, usage FROM sys.user_token ORDER BY principal_id; SELECT IS_MEMBER(N'<group_name>') AS is_group_member;
Important: These queries must be run from a non-admin session to produce meaningful results. An Entra admin session maps to dbo and will not resolve group principals in the token.
Step 5: Compare across databases
Run the Step 1 query on a database where access works and a database where it fails. Compare the mapping_model column for the same group name.
Key Findings
| Finding | Evidence |
|---|---|
| Login-based Entra group users (18-byte SID, AADE suffix) fail with Error 18456 when no matching server login exists | Server login query returned empty; database principal had 18-byte SID |
| Contained Entra group users (16-byte SID) work independently of any server login | Databases with 16-byte SIDs for the same group functioned correctly |
| The Entra admin role masks the issue entirely | Admin sessions map to dbo, bypassing group-based resolution; IS_MEMBER() returns 0 (false negative) |
| Azure SQL caches authentication/permission data for Entra principals | After recreating a principal, access was still denied until DBCC FLUSHAUTHCACHE and DBCC FREESYSTEMCACHE('TokenAndPermUserStore') were executed |
| TRY_CONVERT(uniqueidentifier, sid) returns NULL for 18-byte SIDs | This is expected behavior for login-based users, not evidence of corruption; use SUBSTRING(sid, 1, 16) to extract the object ID |
Root Cause
The Microsoft Entra group was originally provisioned as a login-based database user in several databases. This was done by first creating a server login with CREATE LOGIN [...] FROM EXTERNAL PROVIDER in logical master, then creating the database user with CREATE USER [...] FROM LOGIN [...] in each user database. This produced an 18-byte SID (the Entra group's object ID plus the AADE marker).
At some point after creation, the server login was deleted from logical master (manually or by automation), while the login-based database users were left in place. These database users became orphaned: they reference a login that no longer exists, and Azure SQL cannot resolve the authentication path.
When a user authenticates via an Entra group containing an orphaned login-based database user, the engine:
- Receives the Entra token with the group's object ID
- Finds the login-based database user (18-byte SID with AADE)
- Attempts to resolve the corresponding server login
- Finds no matching server login in master
- Rejects the connection with Error 18456, internal state 5
The issue remained hidden because the primary user testing access was also the Entra admin on the server. The Entra admin connects as dbo in every database, completely bypassing group principal resolution. The orphaned login-based group was never exercised through the normal authentication path until a non-admin connection was attempted.
Resolution
For each affected database, execute the following steps while connected as the Entra admin:
Step 1: Capture existing role memberships
SELECT rp.name AS role_name FROM sys.database_role_members drm JOIN sys.database_principals dp ON dp.principal_id = drm.member_principal_id JOIN sys.database_principals rp ON rp.principal_id = drm.role_principal_id WHERE dp.name = N'<group_name>';
Step 2: Capture explicit permissions
SELECT state_desc, permission_name, class_desc, OBJECT_NAME(major_id) AS object_name FROM sys.database_permissions WHERE grantee_principal_id = ( SELECT principal_id FROM sys.database_principals WHERE name = N'<group_name>' );
Step 3: Drop and recreate as a contained database user
DROP USER [<group_name>]; CREATE USER [<group_name>] FROM EXTERNAL PROVIDER;
Step 4: Re-grant roles and permissions
-- Re-grant roles (adjust based on Step 1 output) ALTER ROLE db_datareader ADD MEMBER [<group_name>]; ALTER ROLE db_datawriter ADD MEMBER [<group_name>]; ALTER ROLE db_ddladmin ADD MEMBER [<group_name>]; -- Re-grant any explicit permissions from Step 2 output
Step 5: Flush authentication cache
DBCC FLUSHAUTHCACHE; DBCC FREESYSTEMCACHE('TokenAndPermUserStore');
Note: Step 5 is essential. Azure SQL caches authentication decisions, and the stale cached entry for the old login-based principal can persist even after the principal is recreated.
Validation Queries
Confirm the principal is now contained
SELECT name, DATALENGTH(sid) AS sid_length, CASE WHEN DATALENGTH(sid) = 18 AND RIGHT(CONVERT(varchar(170), sid, 2), 4) = 'AADE' THEN 'LOGIN-BASED (problem)' ELSE 'CONTAINED (OK)' END AS status, CONVERT(uniqueidentifier, sid) AS entra_object_id FROM sys.database_principals WHERE name = N'<group_name>';
Expected: sid_length = 16, status = CONTAINED (OK), and entra_object_id matches the Entra group's object ID.
Confirm access works without admin
- Remove the test user as Entra admin (or use a different group member who is not admin)
- Connect to the database using SSMS with Microsoft Entra MFA authentication
- Run:
SELECT USER_NAME() AS database_identity, IS_MEMBER(N'<group_name>') AS is_group_member;
Expected: database_identity is not dbo, and is_group_member = 1.
Audit all databases for remaining login-based principals
SELECT DB_NAME() AS database_name, name, type_desc, DATALENGTH(sid) AS sid_length, sys.fn_varbintohexstr(sid) AS sid_hex, CASE WHEN DATALENGTH(sid) = 18 AND RIGHT(CONVERT(varchar(170), sid, 2), 4) = 'AADE' THEN 'LOGIN-BASED (needs fix)' ELSE 'CONTAINED (OK)' END AS status FROM sys.database_principals WHERE type IN ('E', 'X');
Best Practices
- Always use CREATE USER [...] FROM EXTERNAL PROVIDER for Entra principals. This creates a contained database user with a 16-byte SID that works independently of any server login. Avoid CREATE USER [...] FROM LOGIN [...] unless you have a specific requirement for server-level login semantics and understand the dependency.
- Never delete a server login without first converting its login-based database users to contained users. Deleting a server login orphans all login-based users mapped to it across every database on that server.
- Do not rely on Entra admin access to validate group-based authentication. The Entra admin maps directly to dbo in every database and bypasses group principal resolution. Always test with a non-admin group member.
- Standardize provisioning scripts. Different teams or deployment windows using different CREATE USER syntax produces an inconsistent mix of contained and login-based principals that is difficult to audit.
- Periodically audit Entra principals across all databases. Use the audit query from the validation section to detect login-based principals and proactively convert them before they cause access failures.
- Flush the authentication cache after changing principal models. After dropping and recreating an Entra principal, run DBCC FLUSHAUTHCACHE and DBCC FREESYSTEMCACHE('TokenAndPermUserStore') to clear stale cached entries.
Important Considerations
- IS_MEMBER() returns 0 for Entra admin sessions. This is expected behavior because the admin session identity is dbo, not the group user. Do not interpret IS_MEMBER() = 0 as proof that a user is not in the Entra group.
- TRY_CONVERT(uniqueidentifier, sid) returns NULL for 18-byte SIDs. This is expected for login-based users and is not evidence of SID corruption. To extract the Entra object ID from an 18-byte SID, use CONVERT(uniqueidentifier, SUBSTRING(sid, 1, 16)).
- The AADE suffix is a documented format marker, not corruption. Microsoft documentation explicitly states that login-based Entra database users have a SID consisting of the Entra object ID bytes plus AADE.
- USE [database] does not work in Azure SQL Database. You cannot switch between databases within the same session. Open separate connections (or change the database in SSMS Connection Properties) to run diagnostics on master versus user databases.
- Dropping and recreating a user does not preserve role memberships or explicit permissions. Always capture roles and permissions before dropping a principal.
Conclusion
This scenario illustrates how a seemingly simple administrative action, deleting a server login, can create a latent authentication failure that goes undetected for years. The combination of login-based Entra principals, a missing server login, and the masking effect of Entra admin access makes this issue particularly challenging to diagnose.
The fix is straightforward: recreate the affected principals as contained database users and flush the authentication cache. The more important takeaway is prevention: standardize on contained Entra database users, never delete server logins without converting their dependents, and always validate group-based access with a non-admin identity.
Public Microsoft References
| Topic | URL |
|---|---|
| Microsoft Entra authentication overview | https://learn.microsoft.com/azure/azure-sql/database/authentication-aad-overview |
| Microsoft Entra principals (logins, login-based users, contained users) | https://learn.microsoft.com/azure/azure-sql/database/authentication-aad-overview#microsoft-entra-principals |
| CREATE USER (Transact-SQL) | https://learn.microsoft.com/sql/t-sql/statements/create-user-transact-sql |
| Entra users with nonunique display names (WITH OBJECT_ID) | https://learn.microsoft.com/azure/azure-sql/database/authentication-microsoft-entra-create-users-with-nonunique-names |
| sys.database_principals | https://learn.microsoft.com/sql/relational-databases/system-catalog-views/sys-database-principals-transact-sql |
| sys.server_principals | https://learn.microsoft.com/sql/relational-databases/system-catalog-views/sys-server-principals-transact-sql |
| DBCC FLUSHAUTHCACHE | https://learn.microsoft.com/sql/t-sql/database-console-commands/dbcc-flushauthcache-transact-sql |