使用管理 SDK 在 Azure 中创建数据库
Creating a DB in Azure using the Management SDK
我有这段代码可以在 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。我必须创建一个 'cloud service' 吗?
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 中以编程方式创建数据库,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。我必须创建一个 'cloud service' 吗?
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;
}