SivaS
SivaS

Reputation: 27

Entity Framework issue connecting to Azure SQL MI database using Azure AD password authentication

I have a .NET Framework 4.7.2 web app that is using EF6.2 (Entity Framework) on 2012R2 server, connecting to an on-prem SQL server without any issues. Now, I am trying to connect the same app to an Azure SQL MI database from an Azure VM (2019 server) and I get ADAL-related errors. A console application with an Azure AD Password connection string works fine on the same machine. Also, the EF app works fine on the 2012 server, it is the EF version app on the 2019 Azure server VMs that is having the issues. Any pointers in resolving this is really appreciated.

This is the connection string:

<add name="MyEntities" connectionString="metadata=res:///EntityFramework.XXX.csdl|res:///EntityFramework.XXXX.ssdl|res://*/EntityFramework.XXX.msl;provider=System.Data.SqlClient;provider connection string='data source=XXXX.XXXX.database.windows.net;initial catalog=testDB;persist security info=True;user [email protected];password=XXXX;authentication="Active Directory Password";MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />

This is the error:

InnerException: System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> AdalException: The request has timed out. at ADALNativeWrapper.ADALGetAccessToken
...
in XXXXX\source\repos\SampleEF\DataObjects\EntityFramework\XXXX.cs:line 19

Upvotes: 0

Views: 725

Answers (1)

Imran
Imran

Reputation: 5570

This error AdalException: The request has timed out. at ADALNativeWrapper.ADALGetAccessToken" usually occurs, if access to azure ad is blocked by your firewall or expiration of access token.

To resolve this,

  • Try to add additional URLs or IP ranges to firewall.
  • Try using token cache to make sure ADAL.Net refreshes if needed.
  • Try increasing the connection timeout in the web.config while creating the database.
  • Try adding the stored procedure
SET NOCOUNT ON;
SET ARITHABORT ON;
  • If you are using Entity Framework define Time out on Startup class as below:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), o => o.CommandTimeout(180)));
  • Try using the below syntax to change the timeout from the default 30 seconds to 90 seconds:
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
this.Database.SetCommandTimeout(90); // <-- 90 seconds
}}

References :

c# - Entity Framework Timeouts - Stack Overflow by N-ate.

Timeout and ADALGetAccessToken Error with AAD connections - Microsoft Tech Community.

Access token expired Issue · Issue #1596 · AzureAD/azure-activedirectory-library-for-dotnet · GitHub.

Upvotes: 0

Related Questions