如何使用 Active Directory Default 并告诉它使用 Visual Studio?

问题描述 投票:0回答:1

为了在 Visual Studio 或 Visual Studio Code 中调试时本地连接到我们的 Azure SQL 数据库,我们在 SQL 连接字符串中设置

Authentication=Active Directory Default

这会导致一点点缓慢,并在日志中产生大量额外的噪音,因为它尝试连接 3 种不同的方式失败,而这些方式在达到我们实际想要的连接方式之前永远不会起作用。

与 MFA 身份验证相比,我更喜欢这种方式,因此无需 UI 即可登录,但我不知道如何在不使用

Active Directory Default
的情况下通过 Visual Studio/Code 进行连接。

有没有办法使用

Active Directory Default
并指定您实际想要连接的方式,而不是必须通过每种方式,或者是否有其他方法可以有效地完成同样的事情?

authentication azure-sql-database connection-string
1个回答
0
投票

如果 Azure 默认身份验证速度很慢,您可以使用自定义身份验证提供程序并显式配置身份验证源。使用下面的连接字符串:

"Server=tcp:<servername>.database.windows.net,1433; Database=<databasename>;" }

使用以下代码访问数据库的令牌:

var connectionString = Configuration.GetConnectionString("<connectionstringname>");
            services.AddTransient(a =>{
                var sqlConnection = new SqlConnection(connectionString);
                var credential = new DefaultAzureCredential();
                var token = credential
                        .GetToken(new Azure.Core.TokenRequestContext(
                            new[] { "https://database.windows.net/.default" }));
                sqlConnection.AccessToken = token.Token;
                return sqlConnection;

更多信息可以参考这个

© www.soinside.com 2019 - 2024. All rights reserved.