繁体   English   中英

无法使用C#中的AAD连接到Azure SQL数据库

[英]Cannot connect to Azure SQL database using AAD from C#

我们的开发人员最近在Azure SQL服务器中启用了AAD身份验证,并将我的身份(例如my.identity@mycompany.com )作为dbo添加到MyDatabase中 我可以使用SSMS成功连接到数据库,现在我想使用代码中的相同身份进行连接:

using System;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Microsoft.Azure.Services.AppAuthentication;

namespace AzureADAuth {
    class Program {
        static async Task Main(string[] args) {
            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/");

            Principal principal = azureServiceTokenProvider.PrincipalUsed;
            Console.WriteLine($"AppId: {principal.AppId}");
            Console.WriteLine($"IsAuthenticated: {principal.IsAuthenticated}");
            Console.WriteLine($"TenantId: {principal.TenantId}");
            Console.WriteLine($"Type: {principal.Type}");
            Console.WriteLine($"UserPrincipalName: {principal.UserPrincipalName}");

            using (var connection = new SqlConnection("Data Source=########.database.windows.net; Initial Catalog=MyDatabase;")) {
                connection.AccessToken = accessToken;
                await connection.OpenAsync();

                var command = new SqlCommand("select CURRENT_USER", connection);
                using (SqlDataReader reader = await command.ExecuteReaderAsync()) {
                    await reader.ReadAsync();
                    Console.WriteLine(reader.GetValue(0));
                }
            }

            Console.ReadKey();
        }
    }
}

不幸的是,它不起作用。 这是输出:

AppId:
IsAuthenticated: True
TenantId: ########-####-####-bc14-789b44d11a3c
Type: User
UserPrincipalName: my.identity@mycompany.com

Unhandled Exception: System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions,
SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, Sq
lConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTr
ansientFaultHandling)

AzureServiceTokenProvider从Azure CLI成功获取了我的身份,但是SQL登录失败。 我想念什么?

PS

我的目标是.NET完整框架4.7; Microsoft.Azure.Services.AppAuthentication软件包是最新的1.0.1; 我的计算机未加入任何域(不确定是否很重要,因为我不需要AAD集成身份验证,仅基于令牌)

尝试使用database.windows.net而不是management.azure.com选项,

string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://database.windows.net/");

对于有此问题的任何人-我在用户遇到此确切问题而没有解决方案的情况下搜寻了多个资源。 但是,最终能够通过添加SQL数据库所在的“承租人”来解决我的问题。 由于我有多个Azure订阅。

在上面的代码中获取令牌:

return provider.GetAccessTokenAsync("https://database.windows.net/");

我还需要添加租户:

return provider.GetAccessTokenAsync("https://database.windows.net/", "hosttenant.onmicrosoft.com");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM