Gandalf actually entered Dol Guldur on two occasions. The
Tale of Years indicates he first went there in TA 2063, and that "Sauron retreats and hides in the East". I would surmise that Sauron feared to be discovered at that point, as he was still "rebuilding" his power, and was forced to flee, whether he perceived Gandalf's true nature, or not.
The second incursion by Gandalf was in 2850. It was then that he "discovers that [Dol Guldur's] master is indeed Sauron".
In
Unfinished Tales,
The Quest of Erebor, Gandalf says this to Frodo, Merry, Pippin, and Gimli:
Quote:
"I remembered a dangerous journey of mine, ninety-one years before, when I had entered Dol Guldur in disguise..."
|
So Gandalf was disguised, though specifically how we are not told. If there were guards at the door, presumably the disguise was adequate enough to enable his passage inside. Perhaps he had help there from that "master of shapes and changes of hue", Radagast.
I don't think he was there for long. It was certainly less than a year, according to the
TOY, and common sense would impel him to be as quick as possible. I would think Gandalf knew what to look for in identifying Sauron. For one thing, he could have spoken with, or eavesdropped on, any of Sauron's servants there and potentially learned something of what the Power intended to do. From speaking with Elrond, Gandalf would have known physical characteristics of Sauron. Even though Sauron had been forced to rebody himself after his defeat in the Last Alliance, his new form would likely have been very similar to his last. He would have been missing a finger, something a daring Gandalf could have seen.
As for Sauron being aware of Gandalf, it just seems that he wasn't. Perhaps it was a combination of a good disguise on Gandalf's part, and Sauron being preoccupied with plotting, and ruminating over the last of the Seven he'd just taken from Thráin II.