Securely accessing SQL Database from Azure Functions using Managed Service Identity

In one of our recipes, Azure SQL Database interactions using Azure Functions, from Chapter 3, Seamless Integration of Azure Functions with Azure Services, we learned how to access a SQL Database and its objects from Azure Functions by providing the connection string (username and password).

Let's say that, for some reason, you change the password to an account, meaning that any applications using that account wouldn't be able to gain access. As a developer, wouldn't it be good if there was a facility where you didn't need to worry about the credentials and, instead, the framework took care of authentication? In this recipe, we will learn how to access a SQL Database from an Azure Function without providing a user ID or password by using a feature called Managed Service Identity. 

At the time of writing this recipe, the code related to retrieving the access token was available only with Azure Functions V1 (.NET framework) but not with V2 (.NET Core). By the time you are reading this book, it might be available in the latest version of the .NET Core framework, and so this recipe should work with Azure Functions v2 runtime as well.