-
-
Save jessebarocio/c2d871cd565de5b504eaf54ca51ca7d9 to your computer and use it in GitHub Desktop.
Using Azure.Identity to Connect to Azure SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// No credentials needed in the connection string! | |
var connString = "Server=myserver.database.windows.net;Database=MyDatabase"; | |
var cred = new DefaultAzureCredential(); | |
// Get access token. The scope for Azure SQL is https://database.windows.net. | |
var token = cred.GetToken(new TokenRequestContext( | |
new string[] { "https://database.windows.net" })); | |
using(var connection = new SqlConnection(connString)) | |
{ | |
// Set the token | |
conn.AccessToken = token.Token; | |
conn.Open(); | |
// Execute your SQL commands | |
var count = conn.ExecuteScalar<int>("SELECT COUNT(*) FROM Tenants"); | |
Console.WriteLine($"There are {count} tenants"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment