Recently, multifocal transcranial current stimulation (tCS) devices using several relatively small electrodes have been used to achieve more focal stimulation of specific cortical targets. However, it is becoming increasingly recognized that many behavioral manifestations of neurological and psychiatric disease are not solely the result of abnormality in one isolated brain region but represent alterations in brain networks. In this paper we describe a method for optimizing the configuration of multifocal tCS for stimulation of brain networks, represented by spatially extended cortical targets. We show how, based on fMRI, PET, EEG or other data specifying a target map on the cortical surface for excitatory, inhibitory or neutral stimulation and a constraint on the maximal number of electrodes, a solution can be produced with the optimal currents and locations of the electrodes. The method described here relies on a fast calculation of multifocal tCS electric fields (including components normal and tangential to the cortical boundaries) using a five layer finite element model of a realistic head. Based on the hypothesis that the effects of current stimulation are to first order due to the interaction of electric fields with populations of elongated cortical neurons, it is argued that the optimization problem for tCS stimulation can be defined in terms of the component of the electric field normal to the cortical surface. Solutions are found using constrained least squares to optimize current intensities, while electrode number and their locations are selected using a genetic algorithm. For direct current tCS (tDCS) applications, we provide some examples of this technique using an available tCS system providing 8 small Ag/AgCl stimulation electrodes. We demonstrate the approach both for localized and spatially extended targets defined using rs-fcMRI and PET data, with clinical applications in stroke and depression. Finally, we extend these ideas to more general stimulation protocols, such as alternating current tCS (tACS).