private static bool ValidateResourceIsVNetOrSubnet(AzureResourceIdentifier resourceId)
 {
     return(resourceId.Provider.Equals(VNetProvider, StringComparison.InvariantCultureIgnoreCase) &&
            resourceId.Type.Equals(VNetType, StringComparison.InvariantCultureIgnoreCase) &&
            (String.IsNullOrWhiteSpace(resourceId.ChildType) ||
             resourceId.ChildType.Equals(SubnetType, StringComparison.InvariantCultureIgnoreCase)));
 }
        public static ClientContext Build(string tenantId, string vaultMgmtAppId, string vaultMgmtAppSecret, string objectId, string subscriptionId, string vaultResourceGroupName, string location, string vaultName, string vnetSubnetResourceId)
        {
            if (String.IsNullOrWhiteSpace(tenantId))
            {
                throw new ArgumentException(nameof(tenantId));
            }
            if (String.IsNullOrWhiteSpace(vaultMgmtAppId))
            {
                throw new ArgumentException(nameof(vaultMgmtAppId));
            }
            if (String.IsNullOrWhiteSpace(vaultMgmtAppSecret))
            {
                throw new ArgumentException(nameof(vaultMgmtAppSecret));
            }
            if (String.IsNullOrWhiteSpace(objectId))
            {
                throw new ArgumentException(nameof(objectId));
            }
            if (String.IsNullOrWhiteSpace(subscriptionId))
            {
                throw new ArgumentException(nameof(subscriptionId));
            }
            if (String.IsNullOrWhiteSpace(vaultResourceGroupName))
            {
                throw new ArgumentException(nameof(vaultResourceGroupName));
            }
            if (String.IsNullOrWhiteSpace(vnetSubnetResourceId))
            {
                throw new ArgumentException(nameof(vnetSubnetResourceId));
            }

            var subnetResId = new AzureResourceIdentifier(vnetSubnetResourceId);

            if (!ValidateResourceIsVNetOrSubnet(subnetResId))
            {
                throw new ArgumentException("the specified resource identifier is not a valid virtual network or subnet");
            }

            return(new ClientContext
            {
                TenantId = tenantId,
                VaultMgmtApplicationId = vaultMgmtAppId,
                VaultAccessorObjectId = objectId,
                SubscriptionId = subscriptionId,
                VaultResourceGroupName = vaultResourceGroupName,
                PreferredLocation = location ?? "southcentralus",
                VaultName = vaultName ?? "keyvaultsample",
                VNetName = subnetResId.Name,
                SubnetName = subnetResId.ChildName,
                VNetResourceGroupName = subnetResId.ResourceGroup,
                SubnetResourceId = vnetSubnetResourceId
            });
        }