[英]How to export azure database to blob storage
我需要确切地知道如何使用c#登录到Azure。
我基本上想这样做,但是从代码中:]链接]( https://docs.microsoft.com/zh-cn/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;
}
在使用C#导出Azure数据库之前,曾问过这个问题,但我需要查看实际的代码和有关如何获取连接信息的说明。
我需要查看实际代码以及有关如何获取连接信息的说明。
我建议您按照本教程进行有关注册AAD应用程序和添加密钥的操作。 此外,您还可以遵循“ 使用Azure ARM REST API –获取访问令牌” 。
SqlManagementClient managementClient =新的SqlManagementClient(新的TokenCloudCredentials(subscriptionId,GetAccessToken(tenantId,clientId,secretKey))));
根据您的代码,我假设您正在使用软件包Microsoft.WindowsAzure.Management.Sql ,如果您使用TokenCloudCredentials
,则可能会收到以下错误响应:
AFAIK, Microsoft.WindowsAzure.Management.Libraries需要X509Certificate2身份验证,您需要为SqlManagementClient
构造CertificateCloudCredentials
。 要在您的订阅下上传管理证书,可以遵循上载Azure服务管理证书 。 要检索X509Certificate2
实例,可以从此处开始使用“使用管理证书进行身份验证”部分中的代码段。
对于基于令牌的身份验证,可以使用包Microsoft.Azure.Management.Sql并按以下方式构造SqlManagementClient
:
var sqlManagement = new SqlManagementClient(new TokenCredentials("{access-token}"));
此外,调用AcquireTokenAsync
方法时,需要将资源从https://management.core.windows.net/
更改为https://management.azure.com/
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.