azure-active-directory - 使用托管标识从本地 Azure 函数查询 Azure SQL 数据库

标签 azure-active-directory azure-sql-database azure-functions azure-managed-identity

我想使用托管身份(即连接到 Visual Studio 的用户的身份,而不是在我的连接字符串中提供用户 ID 和密码)从调试中在我的机器上执行的 Azure 函数查询 Azure SQL 数据库。

我关注了this tutorial在 Microsoft 文档上,所以我的 Azure SQL Server 有一个 AD 用户作为管理员,这允许我将权限 (db_datareader) 授予我使用 Azure Function Identity 和我在其中的用户(以及部署在 Azure 中的 Function App)创建的 Azure AD 组.

如果我在 Azure 中部署并运行我的 Azure Function,它能够查询我的数据库并且一切正常。但是当我在本地运行我的 Azure Functions 时,出现以下错误:

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

我的函数代码如下:

    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "test")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");


        using (var connection = new SqlConnection(Environment.GetEnvironmentVariable("sqlConnectionString")))
        {
            connection.AccessToken = await (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net");
            log.LogInformation($"Access token : {connection.AccessToken}");
            try
            {
                await connection.OpenAsync();
                var rows = await connection.QueryAsync<Test>("select top 10 * from TestTable");
                return new OkObjectResult(rows);
            }
            catch (Exception e)
            {
                throw e;
            }

        }
    }

代码正确检索 token ,错误发生在 await connection.OpenAsync() 行。

如果我在 Azure Data Studio 中使用与连接到 Visual Studio 的用户相同的用户(该用户是具有数据库权限的 AD 组成员)打开数据库,我可以毫无问题地连接和查询数据库。

这是已知问题还是我遗漏了什么?

最佳答案

在尝试了您的特定场景后,我测试了很多方法来尝试让它在本地工作。这没有用,给出了与您收到的相同的错误消息。

当出现可能的解决方案时,我与一些人讨论过。我测试了它:它有效!

我的主要问题是我的订阅(和我的用户)是 Microsoft 帐户 (Outlook)。因此,您需要在 GetAccessTokenAsync() 调用中指定 tenantId

显然,对于托管身份,您不必指定 tenantId。对于用户,明确指定它是个好主意。如果是个人 MS 帐户,则必须指定。

我的代码(有点):

var tokenProvider = new AzureServiceTokenProvider();

using (var connection = new SqlConnection(CONNECTIONSTRING))
using (var command = new SqlCommand(QUERY, connection))
{
    connection.AccessToken = await tokenProvider.GetAccessTokenAsync("https://database.windows.net/", "<YOUR_TENANT_ID>");

    await connection.OpenAsync();
    var result = (await command.ExecuteScalarAsync()).ToString();

    return new OkObjectResult(result);
}

此解决方案已经过测试,在指定 tenantId(或目录 ID,租户的 GUID)和“onmicrosoft”名称(xxx.onmicrosoft.com).

关于azure-active-directory - 使用托管标识从本地 Azure 函数查询 Azure SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490505/

相关文章:

azure-active-directory - 如何验证用户是否属于 Azure AD 中的组?

azure - 无法使用 Microsoft graph API 获取照片

c# - Azure 耐用功能错误 "No activity functions are currently registered!"

azure - 如何使用 Azure Key-Vault 检索连接字符串,然后将其设置为 AzureWebJobsServiceBus for ServiceBusTrigger

sql-server - 如何最好地结合 SQL Server 数据从 Active Directory 检索用户数据

azure - 从 Azure 功能调用 Azure Active Directory 密码重置策略

azure - 是否可以在 Azure VIP 交换期间更改连接字符串

Azure - 使用 Transact-SQL 和 SSMS 将现有 SQL 数据库复制到另一台服务器

sql-server - 使故障转移组中的 Azure SQL 数据库脱机

java - Azure Spring Boot功能-如何将触发器和输入绑定(bind)同时传递给handleRequest方法?