由于接受的答案有点过时,如果您在 2020 年或更晚的时间在这里,设置集成身份验证的正确方法如下:
(摘自此处,asp.net标准实现)
https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi
-
添加 Microsoft.Azure.Services.AppAuthentication nuget 包。
-
通过添加以下内容来修改您的 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="server=tcp:MyDB.database.windows.net;database=MyDB;UID=AnyString;Authentication=Active Directory Interactive;"" 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 --- 内部异常堆栈跟踪结束
一开始这个错误没有意义,但是一旦你看到参数从数据源重命名为服务器,它就有意义了。