Ejemplo n.º 1
0
        public void CertAuthWithSPN(string cnnStr)
        {
            LiteralCnnString = cnnStr;
            ServiceClientCredentials svcClientCred = null;

            X509Certificate2 localCert = null;
            X509Store        store     = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            try
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCol          = store.Certificates;
                X509Certificate2Collection certsWithSubject = certCol.Find(X509FindType.FindBySubjectName, "TestCertForAuthLib", false);
                localCert = certsWithSubject[0];
            }
            finally
            {
                store.Close();
            }

            ActiveDirectoryServiceSettings aadServiceSettings = new ActiveDirectoryServiceSettings()
            {
                AuthenticationEndpoint = new Uri(CloudEndPoints.AADAuthUri.ToString() + TenantId),
                TokenAudience          = CloudEndPoints.AADTokenAudienceUri
            };

            ClientAssertionCertificate certAssertion = new ClientAssertionCertificate(ClientId, localCert);

            svcClientCred = ApplicationTokenProvider.LoginSilentWithCertificateAsync(TenantId, certAssertion, aadServiceSettings).GetAwaiter().GetResult();
            Assert.NotNull(svcClientCred);
        }
        public static ServiceClientCredentials GetCredentialFromCertificate(string clientId, X509Certificate2 cert)
        {
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

#if net452
            return(ApplicationTokenProvider.LoginSilentWithCertificateAsync(DomainOrTenantId, new ClientAssertionCertificate(clientId, cert), ServiceSettings).GetAwaiter().GetResult());
#endif

#if !net452
            return(ApplicationTokenProvider.LoginSilentWithCertificateAsync(DomainOrTenantId, new Microsoft.Rest.Azure.Authentication.ClientAssertionCertificate(clientId, cert), ServiceSettings).GetAwaiter().GetResult());
#endif
        }
Ejemplo n.º 3
0
        private static ServiceClientCredentials GetCredsServicePrincipalCertificate(string domain, Uri tokenAudience, string clientId, X509Certificate2 certificate)
        {
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

            var clientAssertionCertificate = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate(clientId, certificate);
            var serviceSettings            = ActiveDirectoryServiceSettings.Azure;

            serviceSettings.TokenAudience = tokenAudience;

            var creds = ApplicationTokenProvider.LoginSilentWithCertificateAsync(domain, clientAssertionCertificate, serviceSettings).GetAwaiter().GetResult();

            return(creds);
        }
Ejemplo n.º 4
0
        static void runSample(string tenantId, string subscriptionId, string servicePrincipalId, string servicePrincipalSecret, string location, string armEndpoint, string certPath)
        {
            var resourceGroupName  = "azure-sample-csharp-vm";
            var vmName             = SdkContext.RandomResourceName("vmDotnetSdk", 24);
            var vmNameManagedDisk  = SdkContext.RandomResourceName("vmManagedDotnetSdk", 24);
            var vnetName           = SdkContext.RandomResourceName("vnetDotnetSdk", 24);
            var subnetName         = SdkContext.RandomResourceName("subnetDotnetSdk", 24);
            var subnetAddress      = "10.0.0.0/24";
            var vnetAddresses      = "10.0.0.0/16";
            var ipName             = SdkContext.RandomResourceName("ipDotnetSdk", 24);
            var nicName            = SdkContext.RandomResourceName("nicDotnetSdk", 24);;
            var diskName           = SdkContext.RandomResourceName("diskDotnetSdk", 24);
            var storageAccountName = SdkContext.RandomResourceName("storageaccount", 18);
            var username           = "******";
            var password           = "******";

            Console.WriteLine("Get credential token");
            var adSettings  = getActiveDirectoryServiceSettings(armEndpoint);
            var certificate = new X509Certificate2(certPath, servicePrincipalSecret);
            var credentials = ApplicationTokenProvider.LoginSilentWithCertificateAsync(tenantId, new ClientAssertionCertificate(servicePrincipalId, certificate), adSettings).GetAwaiter().GetResult();

            Console.WriteLine("Instantiate resource management client");
            var rmClient = GetResourceManagementClient(new Uri(armEndpoint), credentials, subscriptionId);

            Console.WriteLine("Instantiate storage account client");
            var storageClient = GetStorageClient(new Uri(armEndpoint), credentials, subscriptionId);

            Console.WriteLine("Instantiate network client");
            var networkClient = GetNetworkClient(new Uri(armEndpoint), credentials, subscriptionId);

            Console.WriteLine("Instantiate compute client");
            var computeClient = GetComputeClient(new Uri(armEndpoint), credentials, subscriptionId);

            // Create a resource group
            try
            {
                Console.WriteLine("Create resource group");
                var rmTask = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    new ProfileResourceManager.Models.ResourceGroup
                {
                    Location = location
                });
                rmTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create resource group. Exception: {0}", ex.Message));
            }

            // Create a Storage Account
            var storageAccount = new ProfileStorage.Models.StorageAccount();

            try
            {
                Console.WriteLine(String.Format("Creating a storage account with name:{0}", storageAccountName));
                var storageProperties = new ProfileStorage.Models.StorageAccountCreateParameters
                {
                    Location = location,
                    Kind     = ProfileStorage.Models.Kind.Storage,
                    Sku      = new ProfileStorage.Models.Sku(ProfileStorage.Models.SkuName.StandardLRS)
                };

                var storageTask = storageClient.StorageAccounts.CreateWithHttpMessagesAsync(resourceGroupName, storageAccountName, storageProperties);
                storageTask.Wait();
                storageAccount = storageTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create storage account {0}. Exception: {1}", storageAccountName, ex.Message));
            }

            var subnet = new ProfileNetwork.Models.Subnet();

            // Create virtual network
            try
            {
                Console.WriteLine("Create vitual network");
                var vnet = new ProfileNetwork.Models.VirtualNetwork
                {
                    Location     = location,
                    AddressSpace = new ProfileNetwork.Models.AddressSpace
                    {
                        AddressPrefixes = new List <string> {
                            vnetAddresses
                        }
                    }
                };
                var vnetTask = networkClient.VirtualNetworks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vnetName,
                    vnet);
                vnetTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual network. Exception: {0}", ex.Message));
            }

            // Create subnet in the virtual network
            try
            {
                Console.WriteLine("Create a subnet");
                var subnetTask = networkClient.Subnets.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vnetName, subnetName, new ProfileNetwork.Models.Subnet
                {
                    AddressPrefix = subnetAddress,
                    Name          = subnetName
                });
                subnetTask.Wait();
                subnet = subnetTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create subnet. Exception: {0}", ex.Message));
            }

            // Create a public address
            var ip = new ProfileNetwork.Models.PublicIPAddress();

            try
            {
                Console.WriteLine("Create IP");
                var ipProperties = new ProfileNetwork.Models.PublicIPAddress
                {
                    Location = location,
                    PublicIPAllocationMethod = ProfileNetwork.Models.IPAllocationMethod.Dynamic,
                };
                var ipTask = networkClient.PublicIPAddresses.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    ipName,
                    ipProperties);
                ipTask.Wait();
                ip = ipTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create IP. Exception: {0}", ex.Message));
            }

            // Create a network interface
            var nic = new ProfileNetwork.Models.NetworkInterface();
            var vmStorageProfile = new ProfileCompute.Models.StorageProfile();

            try
            {
                Console.WriteLine("Create network interface");
                var nicProperties = new ProfileNetwork.Models.NetworkInterface
                {
                    Location         = location,
                    IpConfigurations = new List <ProfileNetwork.Models.NetworkInterfaceIPConfiguration>
                    {
                        new ProfileNetwork.Models.NetworkInterfaceIPConfiguration
                        {
                            Name = string.Format("{0}-ipconfig", nicName),
                            PrivateIPAllocationMethod = "Dynamic",
                            PublicIPAddress           = ip,
                            Subnet = subnet
                        }
                    }
                };

                var nicTask = networkClient.NetworkInterfaces.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    nicName,
                    nicProperties);
                nicTask.Wait();
                nic = nicTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create network interface. Exception: {0}", ex.Message));
            }

            // Create a data disk
            var disk = new ProfileCompute.Models.Disk();

            try
            {
                Console.WriteLine("Create a data disk");
                var diskProperties = new ProfileCompute.Models.Disk
                {
                    CreationData = new ProfileCompute.Models.CreationData
                    {
                        CreateOption = ProfileCompute.Models.DiskCreateOption.Empty,
                    },
                    Location = location,
                    Sku      = new ProfileCompute.Models.DiskSku
                    {
                        Name = ProfileCompute.Models.StorageAccountTypes.StandardLRS
                    },
                    DiskSizeGB = 1,
                };
                var diskTask = computeClient.Disks.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    diskName,
                    diskProperties);
                diskTask.Wait();
                disk = diskTask.Result.Body;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create data disk. Exception: {0}", ex.Message));
            }

            // VM Hardware profile
            var vmHardwareProfile = new ProfileCompute.Models.HardwareProfile
            {
                VmSize = "Standard_A1"
            };

            // VM OS Profile
            var vmOsProfile = new ProfileCompute.Models.OSProfile
            {
                ComputerName  = vmName,
                AdminUsername = username,
                AdminPassword = password
            };

            // VM Network profile
            var vmNetworkProfile = new ProfileCompute.Models.NetworkProfile
            {
                NetworkInterfaces = new List <ProfileCompute.Models.NetworkInterfaceReference>
                {
                    new ProfileCompute.Models.NetworkInterfaceReference
                    {
                        Id      = nic.Id,
                        Primary = true
                    }
                }
            };

            // VM Storage profile
            string diskUri    = string.Format("{0}test/{1}.vhd", storageAccount.PrimaryEndpoints.Blob, diskName);
            var    osDiskName = "osDisk";
            string osDiskUri  = string.Format("{0}test/{1}.vhd", storageAccount.PrimaryEndpoints.Blob, osDiskName);

            vmStorageProfile = new ProfileCompute.Models.StorageProfile
            {
                OsDisk = new ProfileCompute.Models.OSDisk
                {
                    Name         = osDiskName,
                    CreateOption = ProfileCompute.Models.DiskCreateOptionTypes.FromImage,
                    Caching      = ProfileCompute.Models.CachingTypes.ReadWrite,
                    OsType       = ProfileCompute.Models.OperatingSystemTypes.Linux,
                    Vhd          = new ProfileCompute.Models.VirtualHardDisk
                    {
                        Uri = osDiskUri
                    }
                },
                ImageReference = new ProfileCompute.Models.ImageReference
                {
                    Publisher = "Canonical",
                    Offer     = "UbuntuServer",
                    Sku       = "16.04-LTS",
                    Version   = "latest"
                },
                DataDisks = null
            };

            // Create Linux VM
            var linuxVm = new ProfileCompute.Models.VirtualMachine();

            try
            {
                Console.WriteLine("Create a virtual machine");
                var t1     = DateTime.Now;
                var vmTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vmName,
                    new ProfileCompute.Models.VirtualMachine
                {
                    Location        = location,
                    NetworkProfile  = vmNetworkProfile,
                    StorageProfile  = vmStorageProfile,
                    OsProfile       = vmOsProfile,
                    HardwareProfile = vmHardwareProfile
                });
                vmTask.Wait();
                linuxVm = vmTask.Result.Body;
                var t2 = DateTime.Now;
                vmStorageProfile = linuxVm.StorageProfile;
                Console.WriteLine(String.Format("Create virtual machine {0} took {1} seconds", linuxVm.Id, (t2 - t1).TotalSeconds.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual machine. Exception: {0}", ex.Message));
            }

            // Update - Tag the virtual machine
            try
            {
                Console.WriteLine("Tag virtual machine");
                var vmTagTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, new ProfileCompute.Models.VirtualMachine
                {
                    Location = location,
                    Tags     = new Dictionary <string, string> {
                        { "who-rocks", "java" }, { "where", "on azure stack" }
                    }
                });
                vmTagTask.Wait();
                linuxVm = vmTagTask.Result.Body;
                Console.WriteLine(string.Format("Taged virtual machine {0}", linuxVm.Id));
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not tag virtual machine. Exception: {0}", ex.Message));
            }

            // Update - Add data disk
            try
            {
                Console.WriteLine("Attach data disk to virtual machine");
                string newDataDiskName   = "dataDisk2";
                string newDataDiskVhdUri = string.Format("{0}test/{1}.vhd", storageAccount.PrimaryEndpoints.Blob, newDataDiskName);
                var    dataDisk          = new ProfileCompute.Models.DataDisk
                {
                    CreateOption = ProfileCompute.Models.DiskCreateOptionTypes.Empty,
                    Caching      = ProfileCompute.Models.CachingTypes.ReadOnly,
                    DiskSizeGB   = 1,
                    Lun          = 2,
                    Name         = newDataDiskName,
                    Vhd          = new ProfileCompute.Models.VirtualHardDisk
                    {
                        Uri = newDataDiskVhdUri
                    }
                };
                vmStorageProfile.DataDisks.Add(dataDisk);
                var addTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, new ProfileCompute.Models.VirtualMachine
                {
                    Location       = location,
                    StorageProfile = vmStorageProfile
                });
                addTask.Wait();
                vmStorageProfile = addTask.Result.Body.StorageProfile;
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not add data disk to virtual machine. Exception: {0}", ex.Message));
            }

            // Update - detach data disk
            try
            {
                Console.WriteLine("Detach data disk from virtual machine");
                vmStorageProfile.DataDisks.RemoveAt(0);
                var detachTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, vmName, new ProfileCompute.Models.VirtualMachine {
                    Location       = location,
                    StorageProfile = vmStorageProfile
                });
                detachTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not detach data disk from virtual machine. Exception: {0}", ex.Message));
            }

            // Restart the virtual machine
            try
            {
                Console.WriteLine("Restart virtual machine");
                var restartTask = computeClient.VirtualMachines.RestartWithHttpMessagesAsync(resourceGroupName, vmName);
                restartTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not restart virtual machine. Exception: {0}", ex.Message));
            }

            // Stop(powerOff) the virtual machine
            try
            {
                Console.WriteLine("Power off virtual machine");
                var stopTask = computeClient.VirtualMachines.PowerOffWithHttpMessagesAsync(resourceGroupName, vmName);
                stopTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not power off virtual machine. Exception: {0}", ex.Message));
            }

            // Delete VM
            try
            {
                Console.WriteLine("Delete virtual machine");
                var deleteTask = computeClient.VirtualMachines.DeleteWithHttpMessagesAsync(resourceGroupName, vmName);
                deleteTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not delete virtual machine. Exception: {0}", ex.Message));
            }

            // VM Storage profile managed disk
            vmStorageProfile = new ProfileCompute.Models.StorageProfile
            {
                DataDisks = new List <ProfileCompute.Models.DataDisk>
                {
                    new ProfileCompute.Models.DataDisk
                    {
                        CreateOption = ProfileCompute.Models.DiskCreateOptionTypes.Attach,
                        ManagedDisk  = new ProfileCompute.Models.ManagedDiskParameters
                        {
                            StorageAccountType = ProfileCompute.Models.StorageAccountTypes.StandardLRS,
                            Id = disk.Id
                        },
                        Caching    = ProfileCompute.Models.CachingTypes.ReadOnly,
                        DiskSizeGB = 1,
                        Lun        = 1,
                        Name       = diskName,
                    }
                },
                OsDisk = new ProfileCompute.Models.OSDisk
                {
                    Name         = osDiskName,
                    CreateOption = ProfileCompute.Models.DiskCreateOptionTypes.FromImage,
                },
                ImageReference = new ProfileCompute.Models.ImageReference
                {
                    Publisher = "Canonical",
                    Offer     = "UbuntuServer",
                    Sku       = "16.04-LTS",
                    Version   = "latest"
                }
            };

            // Create Linux VM with managed disks
            var linuxVmManagedDisk = new ProfileCompute.Models.VirtualMachine();

            try
            {
                Console.WriteLine("Create a virtual machine with managed disk");
                var t1     = DateTime.Now;
                var vmTask = computeClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroupName,
                    vmNameManagedDisk,
                    new ProfileCompute.Models.VirtualMachine
                {
                    Location        = location,
                    NetworkProfile  = vmNetworkProfile,
                    StorageProfile  = vmStorageProfile,
                    OsProfile       = vmOsProfile,
                    HardwareProfile = vmHardwareProfile
                });
                vmTask.Wait();
                linuxVmManagedDisk = vmTask.Result.Body;
                var t2 = DateTime.Now;
                vmStorageProfile = linuxVm.StorageProfile;
                Console.WriteLine(String.Format("Create virtual machine with managed disk {0} took {1} seconds", linuxVmManagedDisk.Id, (t2 - t1).TotalSeconds.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create virtual machine with managed disk. Exception: {0}", ex.Message));
            }

            // Delete VM with managed disk
            try
            {
                Console.WriteLine("Delete virtual machine with managed disk");
                var deleteTask = computeClient.VirtualMachines.DeleteWithHttpMessagesAsync(resourceGroupName, vmNameManagedDisk);
                deleteTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not delete virtual machine with managed disk. Exception: {0}", ex.Message));
            }

            // Delete a resource group.
            try
            {
                Console.WriteLine(String.Format("Deleting resource group with name: {0}", resourceGroupName));
                var rmDeleteTask = rmClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroupName);
                rmDeleteTask.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not delete resource group {0}. Exception: {1}", resourceGroupName, ex.Message));
            }
        }
        static void runSample(string tenantId, string subscriptionId, string servicePrincipalId, string servicePrincipalSecret, string location, string armEndpoint, string certPath)
        {
            var resourceGroup1Name = SdkContext.RandomResourceName("rgDotnetSdk", 24);
            var resourceGroup2Name = SdkContext.RandomResourceName("rgDotnetSdk", 24);

            Console.WriteLine("Get credential token");
            var adSettings  = getActiveDirectoryServiceSettings(armEndpoint);
            var certificate = new X509Certificate2(certPath, servicePrincipalSecret);
            var credentials = ApplicationTokenProvider.LoginSilentWithCertificateAsync(tenantId, new ClientAssertionCertificate(servicePrincipalId, certificate), adSettings).GetAwaiter().GetResult();

            Console.WriteLine("Instantiate resource management client");
            var rmClient = GetResourceManagementClient(new Uri(armEndpoint), credentials, subscriptionId);

            // Create resource group.
            try
            {
                Console.WriteLine(String.Format("Creating a resource group with name:{0}", resourceGroup1Name));
                var rm = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroup1Name,
                    new Profile2018ResourceManager.Models.ResourceGroup
                {
                    Location = location
                }).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create resource group {0}. Exception: {1}", resourceGroup1Name, ex.Message));
            }

            // Update the resource group.
            try
            {
                Console.WriteLine(String.Format("Updating the resource group with name:{0}", resourceGroup1Name));
                var rmTag = rmClient.ResourceGroups.PatchWithHttpMessagesAsync(resourceGroup1Name, new Profile2018ResourceManager.Models.ResourceGroup
                {
                    Tags = new Dictionary <string, string> {
                        { "DotNetTag", "DotNetValue" }
                    }
                }).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not tag resource grooup {0}. Exception: {1}", resourceGroup1Name, ex.Message));
            }

            // Create another resource group.
            try
            {
                Console.WriteLine(String.Format("Creating a resource group with name:{0}", resourceGroup2Name));
                var rmNew = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(
                    resourceGroup2Name,
                    new Profile2018ResourceManager.Models.ResourceGroup
                {
                    Location = location
                }).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not create resource group {0}. Exception: {1}", resourceGroup2Name, ex.Message));
            }

            // List resource groups.
            try
            {
                Console.WriteLine("Listing all resource groups.");
                var rmList = rmClient.ResourceGroups.ListWithHttpMessagesAsync().GetAwaiter().GetResult();

                var resourceGroupResults = rmList.Body;
                foreach (var result in resourceGroupResults)
                {
                    Console.WriteLine(String.Format("Resource group name:{0}", result.Name));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Could not list resource groups. Exception: {0}", ex.Message));
            }

            // Delete a resource group.
            try
            {
                Console.WriteLine(String.Format("Deleting resource group with name:{0}", resourceGroup2Name));
                var rmDelete = rmClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroup2Name).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("Could not delete resource group {0}. Exception: {1}", resourceGroup2Name, ex.Message));
            }
        }