1

我有这个代码,用于以编程方式在 Azure 中创建数据库,来自这里

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。从这个链接

$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。我必须创建“云服务”吗?

4

1 回答 1

1

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

Get-AzurePublishSettingsFile

更多细节在这里

这是以下格式的 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;
}
于 2016-10-06T21:23:27.553 回答