I am trying to configure connecting to azure sql using managed identity. However before, I need to connect to sql azure from visual studio using AD identity.
I have followed steps here.
I have this code in console app.
SqlConnectionStringBuilder builder = new
Microsoft.Data.SqlClient.SqlConnectionStringBuilder();
builder.DataSource = "sqlserveraddress";
builder.InitialCatalog = "dbaddress";
string connstring = builder.ConnectionString;
await using var conn = new Microsoft.Data.SqlClient.SqlConnection(connstring)
{
AccessToken = await GetAzureSqlAccessToken()
} ;
await conn.OpenAsync();
As required, I have added my visual studio App service authentication identity user in sql azure db.
CREATE USER "[email protected]" FROM EXTERNAL
PROVIDER;
ALTER ROLE db_datareader ADD MEMBER
"[email protected]";
ALTER ROLE db_datawriter ADD MEMBER
"[email protected]";
ALTER ROLE db_ddladmin ADD MEMBER
"[email protected]";
GO
Now if i try running my console application, I get following error.
If i try to decode the access token it shows the user I have added to sql azure. [email protected].
What Am I Missing here??


Microsoft.Data.SqlClientyou dont need acquire manually the token, see this article: learn.microsoft.com/en-us/sql/connect/ado-net/sql/…