c# - 使用 Active Directory 密码从 C# 连接到 Sql Azure Db 时遇到问题

标签 c# sql azure crm des

我正在设置 CRM 360 数据导出服务 (DES) 需要连接的 Azure 数据库。我可以使用 SSMS 手动执行此操作并使用我的 Azure 帐户登录。

enter image description here

然后将 DES 服务帐户设置为外部提供商。

从外部提供商创建用户 DynamicsDataExportService

一切都好,但是我希望自动化该过程,因此我需要使用 C# 登录服务并以这种方式连接和运行代码。

我使用以下代码运行 core 2.2 和 System.Data.SqlClient 4.7:

        var cs = "data source=tcp:dvzxfsdg-sql.database.windows.net,1433;initial catalog=dfsdfs-sqldb; Authentication=Active Directory Integrated";
        using (var connection = new SqlConnection(cs))
        {
            connection.Open();

令人烦恼的是它给出了错误

"System.ArgumentException: 'Keyword not supported: 'authentication'.'".

我很确定我犯的错误是相当基本的,但我似乎无法追根究底。因此,我们非常感谢您的任何指点。

最佳答案

您可能想尝试使用SQLConnectionStringBuilder来正确设置连接字符串的格式。这有助于避免使用明文连接字符串时可能遇到的拼写错误和问题。之后,您实际上可以直接在构建器上设置 AuthenticationMethod。

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder ()
    {
        DataSource = "ServerName",
        InitialCatalog = "DatabaseName",
        etc...
    }

builder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
SqlConnection sqlConnection = new SqlConnection(builder.ConnectionString);

如果 SQL 数据库是 Azure SQL 数据库,则您可以查看资源的概述页面并找到“连接字符串”边栏选项卡。这可能有助于解决您所提供的连接字符串所遇到的问题。

查看this answer有关构建连接字符串的更多信息

关于c# - 使用 Active Directory 密码从 C# 连接到 Sql Azure Db 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881087/

相关文章:

sql - 如何查找/命令/sql 查询以查找 PostgreSQL 中可用的最大连接数?

azure - 如何在 Windows Azure 网站上存储文件?

c# - 找不到类型或命名空间名称 'boolean'

c# - MVC4 - 当优化设置为 true 时捆绑不起作用

sql - 对一列进行分组并获取该列的总和

mysql - 使用group by时如何在单列中获取多个值?

azure - 'make' KDIR=/lib/modules/5.15.0-1023-azure/build....(错误退出状态 : 2)

azure - 将 ServiceNow 用户迁移到 Azure AAD

c# - MVC3 HiddenFor 与枚举。输入的值是枚举键而不是值?

c# - 如何从包含 SELECT 语句的字符串中获取表名