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

时间:2016-10-06 20:57:24

标签: azure azure-management-api

我已经在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。我是否必须创建一个云服务'?

1 个答案:

答案 0 :(得分:1)

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;
}