-
-
Save MoimHossain/7c480d68008deeae1bb6c47e65962f7b to your computer and use it in GitHub Desktop.
SQLHelper.cs
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
public static class TokenHelper | |
{ | |
public static Task<String> GetTokenAsync() | |
{ | |
var provider = new AzureServiceTokenProvider(); | |
return provider.GetAccessTokenAsync("https://database.windows.net/"); | |
} | |
} | |
public static class SqlConnectionExtensions | |
{ | |
public async static Task<TPayload> ExecuteScalar<TPayload>(string commandText) | |
where TPayload: class | |
{ | |
var connectionString = "connection string without credentails"; | |
var token = await ADAuthentication.GetSqlTokenAsync(); | |
using (var conn = new SqlConnection(connectionString)) | |
{ | |
conn.AccessToken = token; | |
await conn.OpenAsync(); | |
using (var cmd = new SqlCommand(commandText, conn)) | |
{ | |
var result = await cmd.ExecuteScalarAsync(); | |
return result as TPayload; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment