Created
August 2, 2019 17:12
-
-
Save phsantiago32/181e6b1bcf244d6990f1873a62c8d103 to your computer and use it in GitHub Desktop.
C# snippet for connecting to an Azure DB instance via access token in a .NET Core app
This file contains hidden or 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
{ | |
"DefaultConnection": "Server=tcp:mydbserver.database.windows.net,1433;Database=mydb;" | |
} |
This file contains hidden or 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
using Microsoft.Azure.Services.AppAuthentication; | |
using System.Data.SqlClient; | |
namespace MyApp.Infrastructure.EntityFramework | |
{ | |
public class MyDbContext : DbContext | |
{ | |
public MyDbContext() | |
{ | |
var conn = (SqlConnection)this.Database.GetDbConnection(); | |
conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps: