gpt4 book ai didi

azure - 使用 Management SDK 在 Azure 中创建数据库

转载 作者:行者123 更新时间:2023-12-03 03:09:55 24 4
gpt4 key购买 nike

我有这段代码,用于以编程方式在 Azure 中创建数据库,from here :

public static string subscriptionId = "ec19938f-6348-4182-83cf-091370e65";
public static string base64EncodedCertificate = "???"; // what goes here?
static SubscriptionCloudCredentials getCredentials()
{
return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate)));
}

static void Main(string[] args)
{
SqlManagementClient client = new SqlManagementClient(getCredentials());
client.Databases.Create("mysub1", new Microsoft.WindowsAzure.Management.Sql.Models.DatabaseCreateParameters()
{
Name = "newdbtest",
MaximumDatabaseSizeInGB = 1,
CollationName = "SQL_Latin1_General_CP1_CI_AS",
Edition = "Web"
});

Console.ReadLine();
}

我相信下一步是获取证书并将其上传到 Azure。来自 this link ,

$cert = New-SelfSignedCertificate -DnsName yourdomain.cloudapp.net -CertStoreLocation "cert:\LocalMachine\My"
$password = ConvertTo-SecureString -String "your-password" -Force -AsPlainText
Export-PfxCertificate -Cert $cert -FilePath ".\my-cert-file.pfx" -Password $password

现在我有了证书,如何获取 base64EncodedCertificate 的值?

问题的第二部分:我该如何处理 .cer 文件?即我假设我将其上传到 Azure。我必须创建“云服务”吗?

最佳答案

Pfx 文件不正确。您需要一个扩展名为 .publishsettings 的文件。您可以通过以下命令从 Azure PowerShell 获取该文件:

Get-AzurePublishSettingsFile

有关它的更多详细信息 here

这是以下格式的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<PublishData>
<PublishProfile SchemaVersion="2.0" PublishMethod="AzureServiceManagementAPI">
<Subscription
ServiceManagementUrl="https://management.core.windows.net"
Id="{GUID With subscription ID}"
Name="{Subscription name}"
ManagementCertificate="{Long Base64 encoded value}" />
</PublishProfile>
</PublishData>

您要查找的值是ManagementCertificate

当我做了与您相同的事情时,我已将 .publishsettings 文件包含到部署中,然后在以下代码中读取它:

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using Microsoft.WindowsAzure;


public CertificateCloudCredentials GetCredentials()
{
try
{
var certFileStream = this.GetCertificateString();
var xDocument = XDocument.Load(certFileStream);

var publishProfileElement = xDocument.Descendants("PublishProfile").Single();
var subscriptionElement = publishProfileElement.Descendants("Subscription").Single();

var certificateAttribute = publishProfileElement.Attribute("ManagementCertificate") ?? subscriptionElement.Attribute("ManagementCertificate");
var subscriptionId = subscriptionElement.Attribute("Id").Value;

var cert = new X509Certificate2(Convert.FromBase64String(certificateAttribute.Value));

var cloudCredentials = new CertificateCloudCredentials(subscriptionId, cert);

return cloudCredentials;
}
catch (Exception exception)
{
throw new DomainException("Could not parse publish settings file: {0}", exception.Message);
}
}


private Stream GetCertificateString()
{
var filePath = @"C:\Full\Path\To\file.publishsettings";

var allBytes = File.ReadAllBytes(filePath);

var stream = new MemoryStream(allBytes);

return stream;
}

关于azure - 使用 Management SDK 在 Azure 中创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39905505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com