ADO.Net(Azure AD)错误“不支持关键字:身份验证”;

ADO.Net(Azure AD)错误“不支持关键字:身份验证”;

问题描述:

我正在尝试通过c#代码使用Azure AD凭据连接到Azure数据库。它在我的系统上工作正常。但是,当我将其部署到32位VM时,会显示错误

I am trying to connect to Azure db with Azure AD credentials through c# code (Code is below). It works fine on my system. But when I deploy it to a 32 bit VM, it shows error


不支持关键字:身份验证。

"Keyword not supported : authentication".

虚拟机已安装.Net Framework 4.5(但未安装Visual Studio)。应用程序的目标是.Net Framework 4.5。

The VM has .Net framework 4.5 installed (But not Visual Studio). Application is targeting .Net Framework 4.5.

根据我的观察,框架2.0的system.data不支持SQLConnection类的身份验证关键字。但是我的应用程序的目标是4.5,因此在安装4.5时应该可以正常工作。任何人都可以帮助解决它。下面是我的代码

As per my observations, system.data for framework 2.0 does not support authentication keyword for SQLConnection class. But my application is targetting 4.5 , so it should work fine with 4.5 installed. can anyone help to resolve it. Below is my code

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string ConnectionString =
      @"Data Source=mydatabase.database.windows.net; Authentication=Active Directory Password; UID=user.name@microsoft.contoso.com; PWD=Test@pswd";
                SqlConnection conn = new SqlConnection(ConnectionString);
                conn.Open();
                Console.WriteLine("connected");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }


此答案是关于dotnet核心而不是.NET Framework 4.5 / 6,因此它不能直接回答您的问题。但是,这可能会帮助您将代码部署到客户。

This answer is regarding dotnet core and not .NET Framework 4.5/6, so it does not directly answer your question. However, it may help you with respect to deployment of code to your customers.

我在尝试从dotnet-core连接时遇到了相同的错误。 从Microsoft

I encountered the same error attempting to connect from dotnet-core. From Microsoft:


从.NET Core 2.2开始,Azure Active Directory颁发的访问令牌可用于对Azure SQL数据库进行身份验证。为了支持访问令牌, AccessToken 属性已添加到 SqlConnection 类。要利用AAD身份验证,请下载 System.Data.SqlClient NuGet程序包的4.6版。为了使用此功能,您可以使用 Active Directory获取访问令牌值。 > Microsoft.IdentityModel.Clients.ActiveDirectory NuGet软件包。

Starting with .NET Core 2.2, an access token issued by Azure Active Directory can be used to authenticate to an Azure SQL database. To support access tokens, the AccessToken property has been added to the SqlConnection class. To take advantage of AAD authentication, download version 4.6 of the System.Data.SqlClient NuGet package. In order to use the feature, you can obtain the access token value using the Active Directory Authentication Library for .NET contained in the Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package.