【问题标题】:Can not connect to Azure SQL Server using Active directory integrated authentication in AppService无法使用 AppService 中的 Active Directory 集成身份验证连接到 Azure SQL Server
【发布时间】:2017-10-11 06:15:47
【问题描述】:

我们在 Azure 应用服务上部署了 Web 应用程序。我们的数据库也在 Azure 上,它被配置为使用 AAD 身份验证(我们已经分配了 AAD 管理员)。

我们在网络应用程序中使用下面的连接字符串来使用下面的连接字符串连接到这个服务器和数据库。

数据源=xxxxxxx.database.windows.net;初始 目录=xxxxxxx;持久安全信息=假;身份验证=活动 目录集成

请注意:通过本地系统使用此连接字符串时工作正常。但是当我们在 Azure App Service 中使用这个 conn 字符串时出现以下错误:

无法在 Active 中验证用户 NT Authority\Anonymous Logon 目录(身份验证=ActiveDirectoryIntegrated)。错误代码 0x4BC; state 10 指定域名格式无效

【问题讨论】:

  • 任何更新?如果您觉得我的回答有用/有帮助。请将其标记为答案,以便其他人可以从中受益。
  • 实际上我们放弃了使用 Active Directory Integrate 身份验证的计划,我们创建了 SQL Server 用户来连接到 sql server。此用户仅适用于应用服务。

标签: azure asp.net-core-mvc azure-sql-database azure-web-app-service


【解决方案1】:

根据您的描述,我发现您使用了 Active Directory 集成身份验证

要使用集成的 Windows 身份验证,您的域的 Active Directory 必须与 Azure Active Directory 联合。连接到数据库的客户端应用程序(或服务)必须在用户域凭据下的已加入域的计算机上运行

如果您将 Web 应用程序发布到 Azure,Azure 的 Web 应用程序服务器将不在您域的 Active Directory 中。所以SQL server不会通过auth。

我建议您可以尝试使用 Active Directory 密码身份验证,而不是 Active Directory 集成身份验证。

使用 azure AD 用户名和密码替换连接字符串,如下所示。它会很好用。

Server=tcp:brandotest.database.windows.net,1433;Initial Catalog=bradnotestsql;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication="Active Directory Password";

【讨论】:

  • 这是否存在暴露连接字符串中密码的风险?
  • 注意,Windows 身份验证与 Azure Active Directory 集成身份验证相同
【解决方案2】:

由于接受的答案有点过时,如果您在 2020 年或更晚的时间在这里,设置集成身份验证的正确方法如下:

(摘自此处,asp.net标准实现)

https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi

  1. 添加 Microsoft.Azure.Services.AppAuthentication nuget 包。

  2. 通过添加以下内容来修改您的 web.config:(在 configSections 中)

    <section name="SqlAuthenticationProviders" type="System.Data.SqlClient.SqlAuthenticationProviderConfigurationSection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

(然后)

<SqlAuthenticationProviders>
    <providers>
        <add name="Active Directory Interactive" type="Microsoft.Azure.Services.AppAuthentication.SqlAppAuthenticationProvider, Microsoft.Azure.Services.AppAuthentication" />
    </providers>
</SqlAuthenticationProviders>

请务必注意您在此处使用的名称。然后...您的连接字符串将如下所示:

<add name="MyEntities" connectionString="metadata=res://*/Data.MyDB.csdl|res://*/Data.MyDB.ssdl|res://*/Data.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;server=tcp:MyDB.database.windows.net;database=MyDB;UID=AnyString;Authentication=Active Directory Interactive;&quot;" providerName="System.Data.EntityClient" />

重要说明是您在 SqlAuthenticationProviders 部分中指定的名称必须与您在身份验证的连接字符串中使用的名称完全相同。

其他重要提示是,从旧的连接字符串中,您必须将数据源更改为服务器,并将初始目录更改为数据库。 UID=AnyString 是必须的,否则抛出异常。

不完全按照这些步骤操作会给你带来一个可爱的错误:

System.Data.Entity.Core.EntityException:底层提供程序在打开时失败。 ---> System.AggregateException:发生一个或多个错误。 ---> System.AggregateException:发生一个或多个错误。 ---> AdalException: 指定域名的格式无效。\r\n at ADALNativeWrapper.ADALGetAccessToken(String username, IntPtr password, String stsURL, String servicePrincipalName, ValueType correlationId, String clientId, Boolean* fWindowsIntegrated, Int64& fileTime)\r\n at System.Data.SqlClient.ActiveDirectoryNativeAuthenticationProvider.c__DisplayClass2_0.b__0()\r\n at System.Threading.Tasks.Task`1.InnerInvoke()\r\n at System.Threading.Tasks.Task.Execute()\r\n --- 内部异常堆栈跟踪结束

一开始这个错误没有意义,但是一旦你看到参数从数据源重命名为服务器,它就有意义了。

【讨论】:

  • 问题是关于 Active Directory 集成身份验证,但您的答案是关于 Active Directory 交互式身份验证(最近称为“Azure Active Directory - Universal with MFA”)。它们是不同的东西。
  • @Mike 不同与否,它解决了我的问题!它适用于 Azure 托管标识身份验证。
  • 像魅力一样工作,还要确保您授予应用程序访问数据库的权限 CREATE USER [MyAppName] FROM EXTERNAL PROVIDER; ALTER ROLE db_datareader ADD MEMBER [MyAppName]; ALTER ROLE db_datawriter ADD MEMBER [MyAppName];改变角色 db_ddladmin 添加成员 [MyAppName];去
【解决方案3】:

也许您需要使用的只是令牌(证书)身份验证,如下资源中所述:

https://github.com/Microsoft/sql-server-samples/tree/master/samples/features/security/azure-active-directory-auth/token

尝试按照该资源中的说明向 Azure Active Directory 注册您的应用程序。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2020-02-19
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多