Ejemplo n.º 1
0
        /// <summary>
        /// Create a request to transfer a new domain name
        /// </summary>
        /// <param name="bearerToken">The Azure bearer token</param>
        /// <param name="subscription">Subscription Id for authorization</param>
        /// <param name="domainName">Domain name to transfer. Note that the Azure App Service Domain only supports
        /// purchase of selected domain TLDs.</param>
        /// <param name="transferAuthorization">Authorization code received from current Domain Registrar to authorize this transfer request</param>
        /// <param name="targetResourceGroupName">The Azure Resource Group to place the registered domain in</param>
        /// <param name="autoRenew">If set, Azure will auto-renew the domain every year</param>
        /// <param name="registrationContact">Contact information to use for transfer</param>
        /// <param name="callerIpAddress">IP address of caller for purchase consent agreement</param>
        /// <param name="agreementKeyNames">Name of the keys of terms and conditions that have been agreed to</param>
        /// <param name="useCustomDns">If set, will not use Azure DNS for the domain (if Azure DNS is required later, it will need to be
        /// configured seperately)</param>
        /// <param name="existingAzureZone">If an existing Azure DNS Zone is to be tied to this domain, the resource Uri to that zone</param>
        /// <returns>True if the domain registration task was accepted by Azure. False if not, NULL if there were errors</returns>
        public static async Task <bool?> TransferDomain(string bearerToken, Guid subscription, string domainName, string transferAuthorization,
                                                        string targetResourceGroupName, bool autoRenew,
                                                        DomainRegistrationContact registrationContact, string callerIpAddress, string[] agreementKeyNames,
                                                        bool useCustomDns = false, ResourceUri?existingAzureZone = null)
        {
            if (string.IsNullOrWhiteSpace(bearerToken))
            {
                throw new ArgumentNullException(nameof(bearerToken));
            }
            if (string.IsNullOrWhiteSpace(targetResourceGroupName))
            {
                throw new ArgumentNullException(nameof(targetResourceGroupName));
            }
            if (string.IsNullOrWhiteSpace(transferAuthorization))
            {
                throw new ArgumentNullException(nameof(transferAuthorization));
            }
            if (subscription == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(subscription));
            }
            if ((agreementKeyNames == null) || (!agreementKeyNames.Any()))
            {
                throw new ArgumentNullException(nameof(agreementKeyNames));
            }

            // validate domain name :-)
            AvailabilityRequest _ = new AvailabilityRequest(domainName);

            DomainTransferRequest request = new DomainTransferRequest(transferAuthorization, autoRenew, registrationContact, callerIpAddress, agreementKeyNames);

            if (useCustomDns)
            {
                request.Properties.TypeOfDns = DomainRegistrationDnsTypeEnum.DefaultDomainRegistrarDns;
            }

            if (existingAzureZone != null)
            {
                if ((!existingAzureZone.IsValid) ||
                    (!existingAzureZone.Is(ResourceUriCompareLevel.Provider, "Microsoft.Network")) ||
                    (!existingAzureZone.Is(ResourceUriCompareLevel.Type, "dnszones")))
                {
                    throw new ArgumentException(nameof(existingAzureZone));
                }

                request.Properties.AzureDnsZoneId = existingAzureZone.ToString();
            }

            RestApiResponse response = await RestApiClient.PUT(
                bearerToken,
                $"https://management.azure.com/subscriptions/{subscription:d}/resourceGroups/{targetResourceGroupName}/providers/Microsoft.DomainRegistration/domains/{domainName}",
                CLIENT_API_VERSION,
                null, request,
                new int[] { 200, 202 }
                );

            return(response.WasException ? (bool?)null : response.IsExpectedSuccess);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a request to transfer a new domain name. This overload automatically validates the domain name and picks up the
        /// required consents to populate for the request.
        /// </summary>
        /// <param name="bearerToken">The Azure bearer token</param>
        /// <param name="subscription">Subscription Id for authorization</param>
        /// <param name="domainName">Domain name to transfer. Note that the Azure App Service Domain only supports
        /// purchase of selected domain TLDs.</param>
        /// <param name="transferAuthorization">Authorization code received from current Domain Registrar to authorize this transfer request</param>
        /// <param name="targetResourceGroupName">The Azure Resource Group to place the registered domain in</param>
        /// <param name="autoRenew">If set, Azure will auto-renew the domain every year</param>
        /// <param name="registrationContact">Contact information to use for transfer</param>
        /// <param name="callerIpAddress">IP address of caller for purchase consent agreement</param>
        /// <param name="useCustomDns">If set, will not use Azure DNS for the domain (if Azure DNS is required later, it will need to be
        /// configured seperately)</param>
        /// <param name="existingAzureZone">If an existing Azure DNS Zone is to be tied to this domain, the resource Uri to that zone</param>
        /// <returns>True if the domain registration task was accepted by Azure. False if not, NULL if there were errors</returns>
        public static async Task <bool?> TransferDomain(string bearerToken, Guid subscription, string domainName, string transferAuthorization,
                                                        string targetResourceGroupName, bool autoRenew, DomainRegistrationContact registrationContact, string callerIpAddress,
                                                        bool useCustomDns = false, ResourceUri?existingAzureZone = null)
        {
            if (string.IsNullOrWhiteSpace(bearerToken))
            {
                throw new ArgumentNullException(nameof(bearerToken));
            }
            if (string.IsNullOrWhiteSpace(targetResourceGroupName))
            {
                throw new ArgumentNullException(nameof(targetResourceGroupName));
            }
            if (string.IsNullOrWhiteSpace(transferAuthorization))
            {
                throw new ArgumentNullException(nameof(transferAuthorization));
            }
            if (subscription == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            IList <TopLevelDomain> supportedTlds = await GetSupportedTopLevelDomains(bearerToken, subscription);

            string?tldName = Common.Extensions.GetPossibleTopLevelDomainName(domainName);

            if (string.IsNullOrWhiteSpace(tldName))
            {
                throw new ArgumentNullException($"Could not retrieve TLD of '{domainName}'.");
            }

            TopLevelDomain?tld = supportedTlds.FirstOrDefault(t => t.Name == tldName);

            if (tld == null)
            {
                throw new ArgumentNullException($"The TLD '{tldName}' is not supported at this time.");
            }

            IList <TopLevelAgreement> agreements = await GetRequiredConsents(bearerToken, subscription, tld.Name, true, true);

            string[] agreementNames = agreements.Select(a => a.AgreementCode).ToArray();

            DomainTransferRequest request = new DomainTransferRequest(transferAuthorization, autoRenew, registrationContact, callerIpAddress, agreementNames);

            if (useCustomDns)
            {
                request.Properties.TypeOfDns = DomainRegistrationDnsTypeEnum.DefaultDomainRegistrarDns;
            }

            if (existingAzureZone != null)
            {
                if ((!existingAzureZone.IsValid) ||
                    (!existingAzureZone.Is(ResourceUriCompareLevel.Provider, "Microsoft.Network")) ||
                    (!existingAzureZone.Is(ResourceUriCompareLevel.Type, "dnszones")))
                {
                    throw new ArgumentException(nameof(existingAzureZone));
                }

                request.Properties.AzureDnsZoneId = existingAzureZone.ToString();
            }

            RestApiResponse response = await RestApiClient.PUT(
                bearerToken,
                $"https://management.azure.com/subscriptions/{subscription:d}/resourceGroups/{targetResourceGroupName}/providers/Microsoft.DomainRegistration/domains/{domainName}",
                CLIENT_API_VERSION,
                null, request,
                new int[] { 200, 202 }
                );

            return(response.WasException ? (bool?)null : response.IsExpectedSuccess);
        }