如何将 Azure 数据库导出到 Blob 存储

How to export azure database to blob storage

我需要确切地知道如何使用 C# 登录到 Azure。

我基本上是想这样做,但是从代码来看: ]a link](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-export)

这是我从互联网上复制的代码,试图实现这一点: 但我不知道如何生成令牌。

                SqlManagementClient managementClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId, clientId, secretKey)));

        var exportParams = new DacExportParameters()
        {
            BlobCredentials = new DacExportParameters.BlobCredentialsParameter()
            {
                StorageAccessKey = storageKey,
                Uri = new Uri(baseStorageUri)
            },
            ConnectionInfo = new DacExportParameters.ConnectionInfoParameter()
            {
                ServerName = azureSqlServer,
                DatabaseName = azureSqlDatabase,
                UserName = adminLogin,
                Password = adminPassword
            }
        };
        var exportResult = managementClient.Dac.Export(azureSqlServerName, exportParams);

我有一个 GetToken 函数,但我不知道从哪里获取 租户 + 客户端 ID + 密码

  private static string GetAccessToken(string tenantId, string 
          clientId, string secretKey)
        {
               var authenticationContext = new 

             AuthenticationContext($"https://login.windows.net/{tenantId}");
                   var credential = new ClientCredential(clientId, secretKey);
                  var result =authenticationContext
                 .AcquireTokenAsync("https://management.core.windows.net/",
            credential);

        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        var token = result.Result.AccessToken;
        return token;
    }

之前有人问过这个问题 但我需要查看有关如何获取连接信息的实际代码和说明。

I need to see the actual code and explanation on how to get the connection info.

我建议您遵循此 tutorial about registering your AAD application and adding the secret key. Moreover, you could also follow Using the Azure ARM REST API – Get Access Token

SqlManagementClient managementClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId, clientId, secretKey)));

根据您的代码,我假设您正在使用包 Microsoft.WindowsAzure.Management.Sql,如果您使用 TokenCloudCredentials,您可能会收到以下错误响应:

据我所知,Microsoft.WindowsAzure.Management.Libraries requires the X509Certificate2 authentication, you need to construct the CertificateCloudCredentials for your SqlManagementClient. For uploading a management certificate under your subscription, you could follow Upload an Azure Service Management Certificate. For retrieving the X509Certificate2 instance, you could follow the code snippet under the Authenticate using a management certificate section from here

对于基于令牌的身份验证,您可以使用包 Microsoft.Azure.Management.Sql 并按如下方式构建您的 SqlManagementClient

var sqlManagement = new SqlManagementClient(new TokenCredentials("{access-token}"));

此外,在调用AcquireTokenAsync方法时需要将资源从https://management.core.windows.net/更改为https://management.azure.com/