I have an Azure External Tenant,and a wpf application registered in it. Associated is a Azure SQL database.
Now in the app, the code SignInButton_Click
below succeeds and logs in a user in the tenant successfully. Now I’d like the user to access the database, via something like GetInfoButton_Click
. As it is I get
`Login failed for user ‘
Do I somehow need get the token into GetInfoButton_Click
? How?
If I change the connection string via Authentication=Active Directory Interactive;
things work, in the sense that after the client is asked to log in again, the connection opens. So the user is found there in the db ok, that doesn’t seem to be the problem. But I should be able to get the authorization directly, as the client is already logged in.
using Microsoft.Data.SqlClient;
using Microsoft.Identity.Client;
private async void SignInButton_Click(object sender, RoutedEventArgs e)
{
var app = App.PublicClientApp;
AuthenticationResult authResult = null;
ResultText.Text = string.Empty;
TokenInfoText.Text = string.Empty;
IAccount firstAccount;
var accounts = await app.GetAccountsAsync();
firstAccount = accounts.FirstOrDefault();
try
{
authResult = await app.AcquireTokenSilent(scopes, firstAccount)
.ExecuteAsync();
}
catch(Exception e)
{ //supressed here for space as this is not being called}
}
private async void GetInfoButton_Click(object sender, RoutedEventArgs e)
{
string cstring = @"Server=entraexperimentserver.database.windows.net;Database=EntraExperimentDB;Encrypt=True;Connection Timeout=30;" +
"Authentication=Active Directory Default;";
using var conn = new SqlConnection(cstring);
{
conn.Open();
conn.Close();
}
}