%3CLINGO-SUB%20id%3D%22lingo-sub-1766458%22%20slang%3D%22en-US%22%3ELogins%20in%20secondary%20server%20not%20mapped%20correctly%20from%20primary%20server%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1766458%22%20slang%3D%22en-US%22%3E%3CP%3E%3CBR%20%2F%3ELogin%20error%20received%20after%20initiating%20failover%20between%20Azure%20SQL%20primary%20server%20and%20its%20geo-replicated%20secondary%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CPRE%3E%22The%20server%20principal%20'%26lt%3Busername%26gt%3B'%20is%20not%20able%20to%20access%20the%20database%20'%26lt%3Bdb_name%26gt%3B'%20under%20the%20current%20security%20context.%20Cannot%20open%20database%20'%26lt%3Bdb_name%26gt%3B'%20requested%20by%20the%20login.%20The%20login%20failed.%20Login%20failed%20for%20user%20'%26lt%3Busername%26gt%3B'%22%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20typically%20means%20the%26nbsp%3Blogins%20in%20secondary%20server%20are%20not%20mapped%20correctly%20from%20primary%20server.%20Reasons%20could%20be%20either%20primary%20and%20secondary%20server%20SID%20mismatch%20or%20it%20could%20be%20a%20permissions%20issue%2C%20i.e.%20did%20not%20provide%20proper%20permissions%20to%20database%20in%20primary%20server.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--536980405%22%20id%3D%22toc-hId--536980407%22%3ESteps%26nbsp%3Bto%20handle%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FH4%3E%0A%3CP%3EVerify%20and%20create%20proper%20login%20mapping%20between%20primary%20and%20secondary.%20The%20procedure%20to%20map%20logins%20is%20exemplified%20below.%20I've%20configured%20geo-replication%20for%20my%20P1%20database%20%3CEM%3EAdventureworks%3C%2FEM%3E%20and%20I've%20created%20%3CEM%3Etestlogin.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%20%231%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ERun%20below%20T-SQL%20in%20master%20of%20primary%20server%20to%20identify%20the%20login%20and%20check%26nbsp%3Bthe%20SID%20matching.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20%5Bname%5D%2C%20%5Bsid%5D%0AFROM%20%5Bsys%5D.%5Bsql_logins%5D%0AWHERE%20%5Btype_desc%5D%20%3D%20'SQL_Login'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ename%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bsid%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3Etestuser%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B0x010600000000006400000000000000004B1D98FA1758A440A6AC481292BFECAE%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3Eazure_test%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%200x0106000000000064000000000000000099514D212C5CC44AB7A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%20%232%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%26nbsp%3B%3CEM%3Etestuser%3C%2FEM%3E%20for%20%3CEM%3EAdventureworks%3C%2FEM%3E%20DB%20in%20primary%20server%20and%20provide%20data_reader%20permission.%20You%20can%20skip%20this%20step%20if%20you%20already%20have%20the%20same%20user%20with%20read-only%20permissions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20USER%26nbsp%3Btestuser%26nbsp%3B%0AFOR%20LOGIN%20testuser%0AEXEC%26nbsp%3Bsp_addrolemember%26nbsp%3B'db_datareader'%2C%26nbsp%3B'testuser'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20below%20T-SQL%20on%20%3CEM%3EAdventureworks%3C%2FEM%3E%20DB%20to%20check%20SID%20value%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20%5Bname%5D%2C%20%5Bsid%5D%0AFROM%20%5Bsys%5D.%5Bdatabase_principals%5D%0AWHERE%20%5Btype_desc%5D%20%3D%20'SQL_USER'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ename%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bsid%3C%2FP%3E%0A%3CP%3Edbo%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B0x010600000000016400000000000000002B84948B9D83A54182DE3A5602C009E3%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3Etestuser%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B0x010600000000006400000000000000004B1D98FA1758A440A6AC481292BFECAE%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20can%20see%20%3CEM%3Etestuser%3C%2FEM%3E%20SID%20matches%20with%20logical%20master%20and%20primary%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%20%233%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20matching%20SID%2C%20execute%20below%20T-SQL%20to%20map%20the%20login%20in%20the%20configured%20geo-replicated%20database%20in%20secondary%20server.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELogin%20to%20secondary%20server%20and%20create%20login%20like%20below%20in%20master%20db%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20LOGIN%20testuser1%0AWITH%20PASSWORD%20%3D'****'%2C%0ASID%20%3D%200x010600000000006400000000000000004B1D98FA1758A440A6AC481292BFECAE%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20connection%20to%20the%20database%20should%20now%20be%20successful.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%221.PNG%22%20style%3D%22width%3A%20369px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225641iB1D35E3D3A32A9E6%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%221.PNG%22%20alt%3D%22SSMS%20test%20connection%201%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ESSMS%20test%20connection%201%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20You%20might%20see%20this%20error%20if%20the%20login%20does%20not%20exist%20in%20the%20user%20database.%20In%20this%20case%2C%20the%20error%20is%20likely%20due%20to%20an%20orphaned%20user.%20Comparing%20the%20users%20lists%20between%20master%20and%20user%20db%20should%20reveal%20a%20SID%20mismatch%20between%20the%20login%20in%20the%20master%20database%20and%20the%20user%20in%20the%20user%20database.%26nbsp%3BTo%20map%20an%20orphaned%20user%20to%20a%20login%20which%20already%20exists%20in%20master%2C%20you%20may%20run%20the%20ALTER%20USER%20statement%20in%20the%20user%20database%2C%20specifying%20the%20login%20name%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EALTER%20USER%20%3CUSER_NAME%3E%20WITH%20Login%20%3D%20%3CLOGIN_NAME%3E%3B%3C%2FLOGIN_NAME%3E%3C%2FUSER_NAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3EFor%20more%20details%20on%20how%20to%20configure%20logins%20and%20users%20for%20geo-replicated%20servers%2C%20please%20refer%20to%20the%20following%20article%20-%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Factive-geo-replication-security-configure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConfigure%20and%20manage%20Azure%20SQL%20Database%20security%20for%20geo-restore%20or%20failover%3C%2FA%3E%26nbsp%3B.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorMelaniaNitu_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EThe%20recommendation%20to%20overcome%20the%20incorrect%20mapping%20is%20to%20use%20contained%20users%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EWith%20SQL%20Database%2C%20you%20can%20always%20create%20this%20type%20of%20user%20account.%3C%2FLI%3E%0A%3CLI%3EWith%20SQL%20Managed%20Instance%20supporting%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Fauthentication-aad-configure%23create-contained-users-mapped-to-azure-ad-identities%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22relative-path%22%3EAzure%20AD%20server%20principals%3C%2FA%3E%2C%20you%20can%20create%20user%20accounts%20to%20authenticate%20to%20the%20SQL%20Managed%20Instance%20without%20requiring%20database%20users%20to%20be%20created%20as%20a%20contained%20database%20user.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EWith%20this%20approach%2C%20the%20user%20authentication%20information%20is%20stored%20in%20each%20database%2C%20and%20replicated%20to%20geo-replicated%20databases%20automatically.%3C%2FSTRONG%3E%20However%2C%20if%20the%20same%20account%20exists%20in%20multiple%20databases%20and%20you%20are%20using%20Azure%20SQL%20Authentication%2C%20you%20must%20keep%20the%20passwords%20synchronized%20manually.%20Additionally%2C%20if%20a%20user%20has%20an%20account%20in%20different%20databases%20with%20different%20passwords%2C%20remembering%20those%20passwords%20can%20become%20a%20problem.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20To%20create%20contained%20users%20mapped%20to%20Azure%20AD%20identities%2C%20you%20must%20be%20logged%20in%20using%20an%20Azure%20AD%20account%20that%20is%20an%20administrator%20in%20the%20database%20in%20Azure%20SQL%20Database.%20In%20SQL%20Managed%20Instance%2C%20a%20SQL%20login%20with%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Esysadmin%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bpermissions%20can%20also%20create%20an%20Azure%20AD%20login%20or%20user.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20more%20details%20on%20contained%20users%2C%20please%20check%26nbsp%3B%3CA%20title%3D%22Authorize%20database%20access%20to%20SQL%20Database%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Flogins-create-manage%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAuthorize%20database%20access%20to%20SQL%20Database%3C%2FA%3E%26nbsp%3Band%26nbsp%3B%3CA%20title%3D%22Contained%20Database%20Users%20-%20Making%20Your%20Database%20Portable%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsecurity%2Fcontained-database-users-making-your-database-portable%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EContained%20Database%20Users%20-%20Making%20Your%20Database%20Portable%3C%2FA%3E%26nbsp%3B.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1766458%22%20slang%3D%22en-US%22%3E%3CP%3EAfter%20server%20failover%20user%20cannot%20login%20to%20the%20database%3A%20%3CEM%3E%22The%20server%20principal%20'%3CUSERNAME%3E'%20is%20not%20able%20to%20access%20the%20database%20'%3CDB_NAME%3E'%20under%20the%20current%20security%20context.%20Cannot%20open%20database%20'%3CDB_NAME%3E'%20requested%20by%20the%20login.%20The%20login%20failed.%20Login%20failed%20for%20user%20'%3CUSERNAME%3E'%22%3C%2FUSERNAME%3E%3C%2FDB_NAME%3E%3C%2FDB_NAME%3E%3C%2FUSERNAME%3E%3C%2FEM%3E.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft


Login error received after initiating failover between Azure SQL primary server and its geo-replicated secondary:

"The server principal '<username>' is not able to access the database '<db_name>' under the current security context. Cannot open database '<db_name>' requested by the login. The login failed. Login failed for user '<username>'"

 

This typically means the logins in secondary server are not mapped correctly from primary server. Reasons could be either primary and secondary server SID mismatch or it could be a permissions issue, i.e. did not provide proper permissions to database in primary server.

 

Steps to handle

Verify and create proper login mapping between primary and secondary. The procedure to map logins is exemplified below. I've configured geo-replication for my P1 database Adventureworks and I've created testlogin.

 

Step #1

Run below T-SQL in master of primary server to identify the login and check the SID matching.

 

SELECT [name], [sid]
FROM [sys].[sql_logins]
WHERE [type_desc] = 'SQL_Login'

 

 

name             sid

testuser         0x010600000000006400000000000000004B1D98FA1758A440A6AC481292BFECAE

azure_test      0x0106000000000064000000000000000099514D212C5CC44AB7A

 

Step #2 

 

Create testuser for Adventureworks DB in primary server and provide data_reader permission. You can skip this step if you already have the same user with read-only permissions.

 

CREATE USER testuser 
FOR LOGIN testuser
EXEC sp_addrolemember 'db_datareader', 'testuser'

 

 

Run below T-SQL on Adventureworks DB to check SID value: 

 

SELECT [name], [sid]
FROM [sys].[database_principals]
WHERE [type_desc] = 'SQL_USER'

 

 

name             sid

dbo               0x010600000000016400000000000000002B84948B9D83A54182DE3A5602C009E3

testuser         0x010600000000006400000000000000004B1D98FA1758A440A6AC481292BFECAE

 

We can see testuser SID matches with logical master and primary database.

 

Step #3

 

If you have matching SID, execute below T-SQL to map the login in the configured geo-replicated database in secondary server. 

 

Login to secondary server and create login like below in master db:

 

 

CREATE LOGIN testuser1
WITH PASSWORD ='****',
SID = 0x010600000000006400000000000000004B1D98FA1758A440A6AC481292BFECAE

 

 

The connection to the database should now be successful.

SSMS test connection 1SSMS test connection 1

 

Note: You might see this error if the login does not exist in the user database. In this case, the error is likely due to an orphaned user. Comparing the users lists between master and user db should reveal a SID mismatch between the login in the master database and the user in the user database. To map an orphaned user to a login which already exists in master, you may run the ALTER USER statement in the user database, specifying the login name:

 

ALTER USER <user_name> WITH Login = <login_name>;


For more details on how to configure logins and users for geo-replicated servers, please refer to the following article - Configure and manage Azure SQL Database security for geo-restore or failover .

 

The recommendation to overcome the incorrect mapping is to use contained users:

  • With SQL Database, you can always create this type of user account.
  • With SQL Managed Instance supporting Azure AD server principals, you can create user accounts to authenticate to the SQL Managed Instance without requiring database users to be created as a contained database user.

With this approach, the user authentication information is stored in each database, and replicated to geo-replicated databases automatically. However, if the same account exists in multiple databases and you are using Azure SQL Authentication, you must keep the passwords synchronized manually. Additionally, if a user has an account in different databases with different passwords, remembering those passwords can become a problem.

 

Note: To create contained users mapped to Azure AD identities, you must be logged in using an Azure AD account that is an administrator in the database in Azure SQL Database. In SQL Managed Instance, a SQL login with sysadmin permissions can also create an Azure AD login or user.

 

For more details on contained users, please check Authorize database access to SQL Database and Contained Database Users - Making Your Database Portable .