private void DefineSslBindingFor(IWebApp webApp, string thumbprint, string forHostname)
 {
     webApp.Update()
     .DefineSslBinding()
     .ForHostname(forHostname)
     .WithExistingCertificate(thumbprint)
     .WithSniBasedSsl()
     .Attach()
     .Apply();
 }
Exemple #2
0
 public Task UpdateAppSettingAsync(string key, string value)
 {
     if (IsSlot)
     {
         return(_slot.Update()
                .WithAppSetting(key, value)
                .ApplyAsync());
     }
     else
     {
         return(_app.Update()
                .WithAppSetting(key, value)
                .ApplyAsync());
     }
 }
        public static void RunSample(IAzure azure)
        {
            string suffix   = ".azurewebsites.net";
            string app1Name = SdkContext.RandomResourceName("webapp1-", 20);
            string app2Name = SdkContext.RandomResourceName("webapp2-", 20);
            string app3Name = SdkContext.RandomResourceName("webapp3-", 20);
            string app4Name = SdkContext.RandomResourceName("webapp4-", 20);
            string app1Url  = app1Name + suffix;
            string app2Url  = app2Name + suffix;
            string app3Url  = app3Name + suffix;
            string app4Url  = app4Name + suffix;
            string rgName   = SdkContext.RandomResourceName("rg1NEMV_", 24);

            try
            {
                //============================================================
                // Create a web app with a new app service plan

                Utilities.Log("Creating web app " + app1Name + " in resource group " + rgName + "...");

                IWebApp app1 = azure.WebApps.Define(app1Name)
                               .WithRegion(Region.USWest)
                               .WithNewResourceGroup(rgName)
                               .WithNewWindowsPlan(PricingTier.StandardS1)
                               .WithJavaVersion(JavaVersion.V8Newest)
                               .WithWebContainer(WebContainer.Tomcat8_0Newest)
                               .Create();

                Utilities.Log("Created web app " + app1.Name);
                Utilities.Print(app1);

                //============================================================
                // Set up active directory authentication

                Utilities.Log("Please create an AD application with redirect URL " + app1Url);
                Utilities.Log("Application ID is:");
                string applicationId = Utilities.ReadLine();
                Utilities.Log("Tenant ID is:");
                string tenantId = Utilities.ReadLine();

                Utilities.Log("Updating web app " + app1Name + " to use active directory login...");

                app1.Update()
                .DefineAuthentication()
                .WithDefaultAuthenticationProvider(BuiltInAuthenticationProvider.AzureActiveDirectory)
                .WithActiveDirectory(applicationId, "https://sts.windows.net/" + tenantId)
                .Attach()
                .Apply();

                Utilities.Log("Added active directory login to " + app1.Name);
                Utilities.Print(app1);

                //============================================================
                // Create a second web app

                Utilities.Log("Creating another web app " + app2Name + " in resource group " + rgName + "...");
                IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId);
                IWebApp         app2 = azure.WebApps.Define(app2Name)
                                       .WithExistingWindowsPlan(plan)
                                       .WithExistingResourceGroup(rgName)
                                       .WithJavaVersion(JavaVersion.V8Newest)
                                       .WithWebContainer(WebContainer.Tomcat8_0Newest)
                                       .Create();

                Utilities.Log("Created web app " + app2.Name);
                Utilities.Print(app2);

                //============================================================
                // Set up Facebook authentication

                Utilities.Log("Please create a Facebook developer application with whitelisted URL " + app2Url);
                Utilities.Log("App ID is:");
                string fbAppId = Utilities.ReadLine();
                Utilities.Log("App secret is:");
                string fbAppSecret = Utilities.ReadLine();

                Utilities.Log("Updating web app " + app2Name + " to use Facebook login...");

                app2.Update()
                .DefineAuthentication()
                .WithDefaultAuthenticationProvider(BuiltInAuthenticationProvider.Facebook)
                .WithFacebook(fbAppId, fbAppSecret)
                .Attach()
                .Apply();

                Utilities.Log("Added Facebook login to " + app2.Name);
                Utilities.Print(app2);

                //============================================================
                // Create a 3rd web app with a public GitHub repo in Azure-Samples

                Utilities.Log("Creating another web app " + app3Name + "...");
                IWebApp app3 = azure.WebApps.Define(app3Name)
                               .WithExistingWindowsPlan(plan)
                               .WithNewResourceGroup(rgName)
                               .DefineSourceControl()
                               .WithPublicGitRepository("https://github.com/Azure-Samples/app-service-web-dotnet-get-started")
                               .WithBranch("master")
                               .Attach()
                               .Create();

                Utilities.Log("Created web app " + app3.Name);
                Utilities.Print(app3);

                //============================================================
                // Set up Google authentication

                Utilities.Log("Please create a Google developer application with redirect URL " + app3Url);
                Utilities.Log("Client ID is:");
                string gClientId = Utilities.ReadLine();
                Utilities.Log("Client secret is:");
                string gClientSecret = Utilities.ReadLine();

                Utilities.Log("Updating web app " + app3Name + " to use Google login...");

                app3.Update()
                .DefineAuthentication()
                .WithDefaultAuthenticationProvider(BuiltInAuthenticationProvider.Google)
                .WithGoogle(gClientId, gClientSecret)
                .Attach()
                .Apply();

                Utilities.Log("Added Google login to " + app3.Name);
                Utilities.Print(app3);

                //============================================================
                // Create a 4th web app

                Utilities.Log("Creating another web app " + app4Name + "...");
                IWebApp app4 = azure.WebApps
                               .Define(app4Name)
                               .WithExistingWindowsPlan(plan)
                               .WithExistingResourceGroup(rgName)
                               .Create();

                Utilities.Log("Created web app " + app4.Name);
                Utilities.Print(app4);

                //============================================================
                // Set up Google authentication

                Utilities.Log("Please create a Microsoft developer application with redirect URL " + app4Url);
                Utilities.Log("Client ID is:");
                string clientId = Utilities.ReadLine();
                Utilities.Log("Client secret is:");
                string clientSecret = Utilities.ReadLine();

                Utilities.Log("Updating web app " + app3Name + " to use Microsoft login...");

                app4.Update()
                .DefineAuthentication()
                .WithDefaultAuthenticationProvider(BuiltInAuthenticationProvider.MicrosoftAccount)
                .WithMicrosoft(clientId, clientSecret)
                .Attach()
                .Apply();

                Utilities.Log("Added Microsoft login to " + app4.Name);
                Utilities.Print(app4);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
        /**
         * Azure App Service sample for managing web apps.
         *  - app service plan, web app
         *    - Create 2 web apps under the same new app service plan
         *  - domain
         *    - Create a domain
         *  - certificate
         *    - Upload a self-signed wildcard certificate
         *    - update both web apps to use the domain and the created wildcard SSL certificate
         */
        public static void RunSample(IAzure azure)
        {
            string app1Name   = SdkContext.RandomResourceName("webapp1-", 20);
            string app2Name   = SdkContext.RandomResourceName("webapp2-", 20);
            string rgName     = SdkContext.RandomResourceName("rgNEMV_", 24);
            string domainName = SdkContext.RandomResourceName("jsdkdemo-", 20) + ".com";

            try {
                //============================================================
                // Create a web app with a new app service plan

                Utilities.Log("Creating web app " + app1Name + "...");

                IWebApp app1 = azure.WebApps.Define(app1Name)
                               .WithRegion(Region.USWest)
                               .WithNewResourceGroup(rgName)
                               .WithNewLinuxPlan(PricingTier.StandardS1)
                               .WithBuiltInImage(RuntimeStack.NodeJS_6_9)
                               .Create();

                Utilities.Log("Created web app " + app1.Name);
                Utilities.Print(app1);

                //============================================================
                // Create a second web app with the same app service plan

                Utilities.Log("Creating another web app " + app2Name + "...");
                IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId);
                IWebApp         app2 = azure.WebApps.Define(app2Name)
                                       .WithExistingLinuxPlan(plan)
                                       .WithExistingResourceGroup(rgName)
                                       .WithBuiltInImage(RuntimeStack.NodeJS_6_9)
                                       .Create();

                Utilities.Log("Created web app " + app2.Name);
                Utilities.Print(app2);

                //============================================================
                // Purchase a domain (will be canceled for a full refund)

                Utilities.Log("Purchasing a domain " + domainName + "...");

                IAppServiceDomain domain = azure.AppServices.AppServiceDomains.Define(domainName)
                                           .WithExistingResourceGroup(rgName)
                                           .DefineRegistrantContact()
                                           .WithFirstName("Jon")
                                           .WithLastName("Doe")
                                           .WithEmail("*****@*****.**")
                                           .WithAddressLine1("123 4th Ave")
                                           .WithCity("Redmond")
                                           .WithStateOrProvince("WA")
                                           .WithCountry(CountryISOCode.UnitedStates)
                                           .WithPostalCode("98052")
                                           .WithPhoneCountryCode(CountryPhoneCode.UnitedStates)
                                           .WithPhoneNumber("4258828080")
                                           .Attach()
                                           .WithDomainPrivacyEnabled(true)
                                           .WithAutoRenewEnabled(false)
                                           .Create();
                Utilities.Log("Purchased domain " + domain.Name);
                Utilities.Print(domain);

                //============================================================
                // Bind domain to web app 1

                Utilities.Log("Binding http://" + app1Name + "." + domainName + " to web app " + app1Name + "...");

                app1 = app1.Update()
                       .DefineHostnameBinding()
                       .WithAzureManagedDomain(domain)
                       .WithSubDomain(app1Name)
                       .WithDnsRecordType(CustomHostNameDnsRecordType.CName)
                       .Attach()
                       .Apply();

                Utilities.Log("Finished binding http://" + app1Name + "." + domainName + " to web app " + app1Name);
                Utilities.Print(app1);

                //============================================================
                // Create a self-singed SSL certificate

                var pfxPath = "webapp_" + nameof(ManageLinuxWebAppWithDomainSsl).ToLower() + ".pfx";

                Utilities.Log("Creating a self-signed certificate " + pfxPath + "...");

                Utilities.CreateCertificate(domainName, pfxPath, CertificatePassword);

                Utilities.Log("Created self-signed certificate " + pfxPath);

                //============================================================
                // Bind domain to web app 2 and turn on wild card SSL for both

                Utilities.Log("Binding https://" + app1Name + "." + domainName + " to web app " + app1Name + "...");

                app1 = app1.Update()
                       .WithManagedHostnameBindings(domain, app1Name)
                       .DefineSslBinding()
                       .ForHostname(app1Name + "." + domainName)
                       .WithPfxCertificateToUpload(Path.Combine(Utilities.ProjectPath, "Asset", pfxPath), CertificatePassword)
                       .WithSniBasedSsl()
                       .Attach()
                       .Apply();

                Utilities.Log("Finished binding http://" + app1Name + "." + domainName + " to web app " + app1Name);
                Utilities.Print(app1);

                Utilities.Log("Binding https://" + app2Name + "." + domainName + " to web app " + app2Name + "...");

                app2 = app2.Update()
                       .WithManagedHostnameBindings(domain, app2Name)
                       .DefineSslBinding()
                       .ForHostname(app2Name + "." + domainName)
                       .WithPfxCertificateToUpload(Path.Combine(Utilities.ProjectPath, "Asset", pfxPath), CertificatePassword)
                       .WithSniBasedSsl()
                       .Attach()
                       .Apply();

                Utilities.Log("Finished binding http://" + app2Name + "." + domainName + " to web app " + app2Name);
                Utilities.Print(app2);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
        /**
         * Azure App Service basic sample for managing web apps.
         *  - Create 3 linux web apps under the same new app service plan:
         *    - 1, 2 are in the same resource group, 3 in a different one
         *    - Stop and start 1, restart 2
         *    - Add Java support to app 3
         *  - List web apps
         *  - Delete a web app
         */

        public static void RunSample(IAzure azure)
        {
            string app1Name = SdkContext.RandomResourceName("webapp1-", 20);
            string app2Name = SdkContext.RandomResourceName("webapp2-", 20);
            string app3Name = SdkContext.RandomResourceName("webapp3-", 20);
            string rg1Name  = SdkContext.RandomResourceName("rg1NEMV_", 24);
            string rg2Name  = SdkContext.RandomResourceName("rg2NEMV_", 24);

            try
            {
                //============================================================
                // Create a web app with a new app service plan

                Utilities.Log("Creating web app " + app1Name + " in resource group " + rg1Name + "...");

                IWebApp app1 = azure.WebApps
                               .Define(app1Name)
                               .WithRegion(Region.USWest)
                               .WithNewResourceGroup(rg1Name)
                               .WithNewLinuxPlan(PricingTier.StandardS1)
                               .WithBuiltInImage(RuntimeStack.NodeJS_6_9)
                               .Create();

                Utilities.Log("Created web app " + app1.Name);
                Utilities.Print(app1);

                //============================================================
                // Create a second web app with the same app service plan

                Utilities.Log("Creating another web app " + app2Name + " in resource group " + rg1Name + "...");
                IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId);
                IWebApp         app2 = azure.WebApps
                                       .Define(app2Name)
                                       .WithExistingLinuxPlan(plan)
                                       .WithExistingResourceGroup(rg1Name)
                                       .WithBuiltInImage(RuntimeStack.NodeJS_6_9)
                                       .Create();

                Utilities.Log("Created web app " + app2.Name);
                Utilities.Print(app2);

                //============================================================
                // Create a third web app with the same app service plan, but
                // in a different resource group

                Utilities.Log("Creating another web app " + app3Name + " in resource group " + rg2Name + "...");
                IWebApp app3 = azure.WebApps
                               .Define(app3Name)
                               .WithExistingLinuxPlan(plan)
                               .WithNewResourceGroup(rg2Name)
                               .WithBuiltInImage(RuntimeStack.NodeJS_6_9)
                               .Create();

                Utilities.Log("Created web app " + app3.Name);
                Utilities.Print(app3);

                //============================================================
                // stop and start app1, restart app 2
                Utilities.Log("Stopping web app " + app1.Name);
                app1.Stop();
                Utilities.Log("Stopped web app " + app1.Name);
                Utilities.Print(app1);
                Utilities.Log("Starting web app " + app1.Name);
                app1.Start();
                Utilities.Log("Started web app " + app1.Name);
                Utilities.Print(app1);
                Utilities.Log("Restarting web app " + app2.Name);
                app2.Restart();
                Utilities.Log("Restarted web app " + app2.Name);
                Utilities.Print(app2);

                //============================================================
                // Configure app 3 to have Java 8 enabled
                Utilities.Log("Adding Java support to web app " + app3Name + "...");
                app3.Update()
                .WithJavaVersion(JavaVersion.V8Newest)
                .WithWebContainer(WebContainer.Tomcat8_0Newest)
                .Apply();
                Utilities.Log("Java supported on web app " + app3Name + "...");

                //=============================================================
                // List web apps

                Utilities.Log("Printing list of web apps in resource group " + rg1Name + "...");

                foreach (IWebApp webApp in azure.WebApps.ListByResourceGroup(rg1Name))
                {
                    Utilities.Print(webApp);
                }

                Utilities.Log("Printing list of web apps in resource group " + rg2Name + "...");

                foreach (IWebApp webApp in azure.WebApps.ListByResourceGroup(rg2Name))
                {
                    Utilities.Print(webApp);
                }

                //=============================================================
                // Delete a web app

                Utilities.Log("Deleting web app " + app1Name + "...");
                azure.WebApps.DeleteByResourceGroup(rg1Name, app1Name);
                Utilities.Log("Deleted web app " + app1Name + "...");

                Utilities.Log("Printing list of web apps in resource group " + rg1Name + " again...");
                foreach (IWebApp webApp in azure.WebApps.ListByResourceGroup(rg1Name))
                {
                    Utilities.Print(webApp);
                }
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rg2Name);
                    azure.ResourceGroups.DeleteByName(rg2Name);
                    Utilities.Log("Deleted Resource Group: " + rg2Name);
                    Utilities.Log("Deleting Resource Group: " + rg1Name);
                    azure.ResourceGroups.DeleteByName(rg1Name);
                    Utilities.Log("Deleted Resource Group: " + rg1Name);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }