【问题标题】:Manage Domains of an Azure Web App using C#使用 C# 管理 Azure Web 应用程序的域
【发布时间】:2016-09-27 23:54:38
【问题描述】:

我正在以编程方式使用 C# 创建和配置 Azure Web 应用程序

我已经能够成功创建一个网络应用程序,并且还配置了它的网络托管计划并将其升级为“共享”。不过,我想将域添加到我的网络应用程序

例如,我想将 www.examplesite.com 添加到我的网络应用“TestWebApp”中。

我已经在我的程序中使用了以下命名空间:

  • 使用 Microsoft.WindowsAzure.Management.WebSites;
  • 使用 Microsoft.WindowsAzure.Management.WebSites.Models;

为了实现这一点,我需要哪些细节?谢谢!

【问题讨论】:

    标签: c# azure web-applications dns


    【解决方案1】:

    您可以使用以下代码来管理您的域。

    using System;
    using Microsoft.WindowsAzure.Management.WebSites;
    using Microsoft.WindowsAzure.Management.WebSites.Models;
    using System.Security.Cryptography.X509Certificates;
    using Microsoft.Azure;
    
    namespace updateWebApp
    {
        class Program
        {
            private static WebSiteManagementClient _WebSiteClient;
            private static String SubscriptionId = "<your subscription id>";
            private static X509Certificate2 cert = new X509Certificate2("E:/path/azure.cer");
            private static String webspace = "<your webspace name>";
            private static String websitename = "<your web site name>";
    
            static void Main(string[] args)
            {
                var credential = new CertificateCloudCredentials(SubscriptionId, cert);
                _WebSiteClient = new WebSiteManagementClient(credential);
    
                var web = _WebSiteClient.WebSites.Get(webspace, websitename, null).WebSite;
    
                web.HostNames.Add("www.example.com");
    
                var updates = new WebSiteUpdateParameters{
                    HostNames = web.HostNames,
                    ServerFarm = web.ServerFarm,
                    State = web.State
                };
    
                _WebSiteClient.WebSites.Update(webspaces, websitename, updates);
    
    
                System.Console.WriteLine("Press ENTER to continue");
                System.Console.ReadLine();
            }
        }
    }
    

    这段代码只是将禁用 ssl 的域“www.example.com”添加到您的网络应用程序中。为了使用我的代码,您需要执行以下操作。

    1. 创建证书并将其上传到 Azure 经典门户。

    2. 创建 DNS 记录,如 here 所述。

    3. 如果要启用 ssl,还需要为 WebSiteUpdateParameters 设置 HostNameSslStates。为此,您需要指纹和 VirtualIp。

    【讨论】:

    • 这太棒了!正是我需要的。谢谢!我已经将它应用到我的代码中并且它成功地工作了。 :)
    【解决方案2】:

    在大多数情况下您不需要更改代码,您需要配置 DNS 以将您的用户定向到正确的 IP 地址。

    在以下 msdn 页面上,您可以找到有关如何为您的站点配置自定义域名的演练:web-sites-custom-domain-name

    【讨论】:

    • 好吧,OP 专门询问如何以编程方式进行操作。您提供的链接显示了如何在 Azure 门户中执行此操作。我认为这不是 OP 想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2017-07-05
    • 2017-07-30
    相关资源
    最近更新 更多