"Only active directory users can impersonate other active directory users" when accessing an Azure SQL Database

Not long ago I faced the following error.

System.Data.SqlClient.SqlException (0x80131904): Only active directory users can impersonate other active directory users.
   at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__180_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
...so on...

Everything looked correct (such as the connection string containing the credential to connect the database was correct and the user seemed to have the correct permissions as well) therefore, I spent some time understanding further about SQL.

I did find some MSDN article talking about “EXECUTE AS” however, I still didn’t really know the actual issue and I couldn’t find many other available online articles related to this issue at that time, so hopefully, this article could save someone else’s time.

Eventually, I found out that there is a part in Azure which shows us the detail of our SQL server, such as:
Overview of SQL Server

The above error will occur if the database’s owner is set to be the same with the field “Active Directory admin” and the error can be avoided by using the user in “Server admin”.

You can check the database’s owner by opening the SSMS application to connect to your database server, do right click on the related database, and click “Properties” to see what is the current “Owner” of the database.

One of the ways to change it is to run a SQL query as follows:
ALTER AUTHORIZATION ON DATABASE::YourDatabaseName TO YourDbOwner

Comments

Popular posts from this blog

Added Contact Is Not Shown in List Manager in a Scaled Sitecore XP 9

Windows could not start the Sitecore Xconnect Search Indexer - {xConnectInstance}-IndexWorker service on Local Computer. Error 1064: An exception occurred in the service when handling the control request.

EXM Throws NonCriticalException "Failed to download string content" with Nested Exception of "403 Forbidden" in Azure Web App