Renowned Advaitin Sri Vidyarana Swami had written a biography of Sri Adi Shankara. And this instance is also recorded in it.
Canto 9 of Madhaviya Shankara Digvijaya records this incident. Shankara decides to enter the body of King as:
Sankara said to his disciple Padmapada "Here lies dead the renowned. king Amaruka, having more than a hundred wives of exquisite beauty. I am thinking why I should not enter into the body of this king and revive it by Yogic power, and thus live in his palace amidst these women through his body in order to gain knowledge of sex-love and achieve the status of an all-knowing person. I shall thus have an opportunity to make a direct study, as .a witness, of the manifestation of love in women and their behaviour under its influence."
PadmaPada (who was disciple of Shankara) asks whether it is right to do such or not ?
In reply to this proposal of the great Sannyasin, Padmapada gave his cautious reply as follows: "There is nothing unknown to an omniscient personage like you. Yet, I shall speak a few words out of my love for, and devotion to, you. It seems, in days of yore, a great Yogi named Matsyendra, entrusting his own body to his disciple Goraksha, entered into the body of a dead king and thereby got access into his palace. While the Yogi thus reigned as the king, ptosperity attended that kingdom. Timely rain brought bounteous harvests. Observing all this, it occurred to his ministers that some great soul must have entered into the dead body of the king. So they advised his consorts to use all their amorous skill to keep the king completely absorbed in love sports, so that he might not leave the body. The king got so immersed in the emotional display of these women, their amorous advances, soft laughter, sweet songs and lovely dances-that he forgot everything about Samadhi and spiritual matters, and behaved exactly like a sensuous man. Coming to know of the change that had come over his teacher, the disciple Goraksha, after duly preserving his teacher's body in a secure place, came disguised as a dance instructor and got access to the inner apartment as a dancing instructor for women. He attracted the king's attention, and by instructing him about spiritual truth, he destroyed the king's thirst for sense enjoyments. By regaining his power of Yoga, Matsyendra was able to leave the king's body and re-enter his own. From this it is seen how strong and irre-sistible sense attraction is. Besides, to abandon the vow of Brahmacharya is sinful, too. All these of course are matters too well-known to you. How great and how unparalleled in excellence are our holy vows, and how ignoble and wretched is sexuality? If a personage of your type goes after the latter, the whole world will get degraded by following your example. The Dharma of Sannyasins is already at a low ebb, and it has been your life-work to re-interpret and restore it to its pristine purity. All these are matters very well known to you. It is only love of you that prompts me to speak in this vein."
Then Shankara replies to PadmaPada and justifies his action as Dharmic as:
After Padmapada finished, Sankara who excelled Brihaspati himself in learning, spoke thus: "What you have said is only a partial appraisal of the situation. Now, hear the whole truth about it. In one who is absolutely non-attached, desire for sense enjoyment will not arise, This was the case with Sri Krishna when he lived with the Gopis. The continence of one who knows the Yogic practice of Vajroli will remain unbroken. Sankalpa, the brooding imaginative association, is the cause of desires. I am without that failing, even like Maha-Vishnu. One who is without any Sankalpa may live in Samsara without being affected by it; for the real root of Samsara has already been destroyed. The commandments and prohibitions of scriptures are applicable to men in ignorance who live with the deep-rooted conviction that their body is their self. In the case of one who has realised even here that the Self, which is called 'he', is without all contacts and is the relationless and eternally pure spirit-of one who is established in this supreme teaching of the Vedanta-the commandments and prohibitions of Sastras' have no application. There is only clay in all pots that are made of clay. Similar is the case with all objects born of Paramatman. He and the world supposed to be born of Him are not different, the world having no existence apart from Him. How can one who realises the whole world as a mere appearance, be affected by anything? If a person performs Yagas and Yajnas in dream, will he derive any benefit from it? All fruits are non-existent for one who has realised the world as a mere appearance. Let a hundred Yagas be performed, or let hundreds of men be killed. Neither the good nor the bad effects of such actions will affect a knowing one in the least, as he has no sense of agency with regard to all actions flowing through his instrumentality. The Upanishads say that not a hair of Indra was affected, though he killed Trisiras, the son of Twashta, and offered the bodies of ascetics as food for wolves. The Vedas also say that though king Janaka performed many Yajnas and charities, he did not get further embodiment to enjoy the fruits of these ; for, by virtue of his knowledge he was absorbed in the Bliss of Paramatman for ever. A true knower will thus be free from all sinful effects, like Indra, and from enjoyable fruits like Janaka. For, he is free from any sense of good and evil. So, even if I indulge in the enjoyment of sex-love with this body, no evil will result from it. However, in order that the world may not be misled in respect of virtuous conduct by observing my example of a Sannyasin indulging in the practice of sex-love, my proposal is to gain the experiences of sex-life through the body of another person whose dead body I am going to enliven by temporarily identifying myself with that body."