azure-sdk-for-net
azure-sdk-for-net copied to clipboard
App authentication with ManagedIdentityCredential for MySQL Entity Framework Core
Library name and version
Azure.Identity
Query/Question
I'm unsure whether this is a bug, or something that isn't supported at the moment, or whether this is an issue with entity framework.
I currently use AzureServiceTokenProvider
for app authentication with user managed identity to connect to mysql database like:
var provider = new AzureServiceTokenProvider($"RunAs=App;AppId={managedIdentityClientId}");
var token = await provider.GetAccessTokenAsync(_dbScope);
connection.Password = token;
optionsBuilder.UseMySQL(connection);
This works fine, but then I've read https://learn.microsoft.com/en-us/dotnet/api/overview/azure/app-auth-migration mentioning that AzureServiceTokenProvider
will eventually be deprecated.
Looking at the documents, it says to use new ManagedIdentityCredential(appId)
instead.
However, altering my code to use like:
var cred = new ManagedIdentityCredential(managedIdentityClientId);
var token = await cred .GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { "https://ossrdbms-aad.database.windows.net" }));
connection.Password = token.Token;
optionsBuilder.UseMySQL(connection);
Doesn't work. To be accurate... the token DOES work when I try to connect to the database myself using the same managed identity's user that was set up on server side. But when ran in an azure container app, it will throw an exception of:
System.InvalidOperationException: No database provider has been configured for this DbContext
I've also tried using DefaultAzureCredentials
and have the same result.
As soon as I use AzureServiceTokenProvider
, it works again.
Looking at https://learn.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-azure-database?tabs=mysql%2Cuserassigned%2Cdotnet%2Cps#3-modify-your-code it does give an example with using MySqlClient
.
Am I doing something wrong?
Environment
- .net 6.0
- Azure Container Apps
- MySql.Data.EntityFramework
- Azure Database for MySQL servers
Hi @Garwakizashi. Thank you for reaching out and we regret that you're experiencing difficulties. There's not much context around the error that you're receiving, but based on the message, this appears to be an issue with Entity Framework, unrelated to any of the Azure SDK libraries. A web search turns up quite a few issues, blog entries, Stack Overflow questions. For example, the following in the EF Core repository:
- System.InvalidOperationException: “No database provider has been configured for this DbContext
- InvalidOperationException: No database provider has been configured for this DbContext - Blazor Server Application
If the information in those issues and available articles does not resolve your issue, I'd suggest opening an issue in the EF Core repository with your full stack trace and code snippets.
I'm going to mark this as addressed. If you believe that we're mistaken, please unresolve and provide the full stack trace that you feel indicates and Azure SDK issue and we'll take a look.
Hi @Garwakizashi. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve
” to remove the “issue-addressed” label and continue the conversation.
Hi @Garwakizashi, since you haven’t asked that we “/unresolve
” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve
” to reopen the issue.