0

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.

https://learn.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-sql-database?tabs=windowsclient%2Cef%2Cdotnet#3-modify-your-project

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.

enter image description here

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??

3

1 Answer 1

0

You need to set the created Managed Identity as admin in SQL Server.

Follow this: Azure SQL Server -> Settings -> Azure Active Directory

Now click on Set Admin option and search for the Managed Identity to which you want to give access.

Click on Save.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.