I am able to log in via azure data studios. I have went through multiple tutorials and cannot get my ASP.NET Core code to work to log into the db from the app service or locally. I get "Login failed for user ''."
The steps I have taken. My app service for my web api is set to managed identity. I have added this identity to grp-sqladmin. I have created an app service for a sql database then set managed identity to yes. I have set the admin for the sql db to grp-sqladmin in active directory of the sql database server. I have then added the managed identity to the sql database as a user using the following:
CREATE USER [WebAPI] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [WebAPI];
ALTER ROLE db_datawriter ADD MEMBER [WebAPI];
ALTER ROLE db_ddladmin ADD MEMBER [WebAPI];
GO
I have then made my connection string in my web api :
},
"ConnectionStrings": {
"AzureSqlConnection": "Server=tcp:blahblah.database.windows.net;Database=My DB; Authentication = Active Directory Default;"
},
I draw an error in my dbContext getting the access token:
public class DbContext
{
private readonly IConfiguration _configuration;
private readonly string? _connectionString;
public DbContext(IConfiguration configuration)
{
_configuration = configuration;
_connectionString = _configuration.GetConnectionString("AzureSqlConnection");
}
public IDbConnection CreateConnection()
{
var conn = new SqlConnection(_connectionString);
var credential = new Azure.Identity.DefaultAzureCredential();
var token = credential.GetToken(new Azure.Core.TokenRequestContext(new[] { "https://database.windows.net/.default" }));
conn.AccessToken = token.Token;
return conn;
}
}
When i set the access token for the connection it states: "Cannot set the AccessToken property if 'Authentication' has been specified in the connection string."
Then if I go to remove that authentication=Active Directory Default the token is generated and assigned to the connection but when I run my query I get the error: "Login failed for user ''."
I have wasted hours trying to figure out why this is not working. Any ideas? The tutorial I went by was this: https://learn.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-sql-database?tabs=windowsclient%2Cef%2Cdotnet
public class CalendarRepository : ICalendarRepository
{
private readonly DbContext _context;
public CalendarRepository(DbContext context)
{
_context = context;
}
public async Task<IEnumerable<CalendarEvent>?> GetCalendarEvents()
{
try
{
var query = "SELECT * FROM CALENDAR";
using (var connection = _context.CreateConnection())
{
var events = await connection.QueryAsync<CalendarEvent>(query);
return events.ToList();
}
}
catch(Exception ex)
{
return null;
}
}
}
However, I have watched a few youtube videos and some users do add the authentication item at the end of their connection string so this is confusing. Not sure if this is the primary issue also.
I use managed identies for MS Graph API and Azure key vaults and love it so I would like to integrate this SQL DB stuff into identities as well. I am using system assigned identity as well if that matters.
I do az login in cli and i login using my account. Which I have also included in the grp-sqlamin group so it should all be connected i would think.





