0

What are the steps to successfully connect the application to Azure SQL Database after setting up the connection string and adding the App Authentication NuGet package.

6
  • What have you tried? What practical problem are you facing? Where is your code? P.s. I'm assuming you've done some basic research and found a tutorial like this one: learn.microsoft.com/en-us/azure/azure-sql/database/… or something similar? If not, why not? Commented Jun 16, 2020 at 17:58
  • I did find learn.microsoft.com/en-us/azure/app-service/… but i am looking for something in .Netcore without entity framework. Commented Jun 16, 2020 at 18:15
  • I am stuck and I am unable to find AccessToken method under SqlConnection. How to open the connection to Azure SQL Database? Commented Jun 16, 2020 at 18:19
  • Follow the tutorial I linked you to. It uses .NET Core and ADO.NET Commented Jun 16, 2020 at 19:32
  • Thanks but is there a way to use managed identity (using access token)? Commented Jun 16, 2020 at 19:38

1 Answer 1

3

If you want to use Azure Managed Identity to connect Azure SQL database in .Net Core MVC project, We can use the package Microsoft.Data.SqlClient with SqlConnection.AccessToken.

The detailed steps are as below.

  1. Create MSI

  2. Configure SQL Database

    a. Use your Azure Sql AD admin to connect Azure SQL vai SSMS

    b. Add the MSI to the database you need use

    USE [<db name>]
    GO
    create user [<your msi name>] from external provider
    ALTER ROLE db_owner ADD MEMBER [<function app name>]
    
  3. Code

 /*
             Install SDK Microsoft.Azure.Services.AppAuthentication and Microsoft.Data.SqlClient

*/

 public async Task<IActionResult> Index()
        {
            List<StarWar> starWars = new List<StarWar>();
            var connectionString = "Server=tcp:<server-name>.database.windows.net,1433;Database=<database-name>;";
            using (var conn = new SqlConnection(connectionString))
            {
                conn.AccessToken = await (new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/");
                await conn.OpenAsync();
                var sql = "SELECT  * FROM [dbo].[StarWars]";
                using (SqlCommand command = new SqlCommand(sql, conn))
                {
                    using (SqlDataReader reader = await command.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            StarWar starWar = new StarWar();
                            starWar.episode = Convert.ToInt32(reader["episode"]);
                            starWar.score = Convert.ToInt32(reader["score"]);
                            starWar.name = Convert.ToString(reader["name"]);
                            starWars.Add(starWar);
                        }
                    }

                }
            }


                return View(starWars);
        }

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.