实体框架问题使用Azure AD密码认证连接到Azure SQL MI数据库

发布于 2025-01-18 03:09:09 字数 1324 浏览 0 评论 0 原文

我有一个.NET Framework 4.7.2 Web应用程序,该应用程序在2012R2 Server上使用EF6.2(实体框架),无需任何问题连接到本地SQL Server。现在,我正在尝试将同一应用程序连接到Azure SQL MI数据库(2019 Server),并且会遇到与ADAL相关的错误。具有Azure AD密码连接字符串字符串的控制台应用程序在同一台计算机上正常工作。此外,EF应用程序在2012年服务器上正常运行,它是2019年Azure Server VMS上的EF版本应用程序。任何解决此问题的指示都非常感谢。

这是连接字符串:

< add name =“ myentities” connectionsTring =“ metadata = res:// /entityframework.xxx.csdl| res:// /em>/em>/em>/em>/em>/em>/em>/em> xxxx.sssdl| rese :////* /entityframework.xxx.msl; Provider = System.Data.sqlClient; Provider Connection string string ='data Source = xxxx.xxxxx.database.database.windows.net; onirter catalog; intiral catalog = testdb; testdb; persist Security Info = true = true = true; user

这是错误:

Innerexception:System.AggregateException:发生一个或多个错误。 ---> System.AggregateException:发生一个或多个错误。 ---> Adalexception:请求已计时。在AdalnativeWrapper.AdalgetAccesstoken上
...
in xxxxx \ source \ repos \ sampleef \ dataObjects \ entityframework \ xxxx.cs:行19

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2025-01-25 03:09:09

此错误 adalexception:请求已计时。在AdalnativeWrapper.adalgetaccesstoken上“ 通常会发生,如果您的防火墙会阻止对Azure AD的访问或访问令牌的到期。

解决此问题,

  • 尝试将其他URL或IP范围添加到防火墙。
  • 令牌缓存,以确保需要
  • 在创建数据库时尝试增加web.config中的连接
  • 超时
SET NOCOUNT ON;
SET ARITHABORT ON;
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), o => o.CommandTimeout(180)));
  • 尝试使用 使用下面的语法将超时从默认30秒更改为90秒:
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
this.Database.SetCommandTimeout(90); // <-- 90 seconds
}}

参考:

c# - 实体框架超时 - 堆栈溢出 by n-ate

。 - 和adalgetAccesstoken-error-with-aad-connections/ba-p/368806“ rel =“ nofollow noreferrer”> timeout和aad connections aad Connections-microsoft技术社区。

访问代币过期的问题 - 纤维 - dotnet·github

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文