Note: This question mostly pertains to the film, not the book.
In The Two Towers when Aragorn, Legolas, and Gimli are tracking Merry and Pippin, they discover that the trail leads into Fangorn Forest. Following the trail, Legolas eventually discovers that "the white wizard" is approaching them, whom they naturally assume to be Saruman. After a fruitless attempt to kill him, the white wizard speaks to them, informing them of the Hobbits' passing through the forest, all the while concealing his identity with a blinding white light.
After Aragorn asks who he is, the white wizard reveals himself to be Gandalf.
This is different from the book. When they first encounter Gandalf in Fangorn, he didn't seem to make any deliberate attempt to conceal his identity, they simply just did not recognize him at first.
'You speak as one that knows Fangorn well,' said Aragorn. 'Is that so?'
'Not well,' said the old man: 'that would be the study of many lives. But I come here now and again.'
'Might we know your name, and then hear what it is that you have to say to us?' said Aragorn. 'The morning passes, and we have an errand that will not wait.'
'As for what I wished to say, I have said it: What may you be doing, and what tale can you tell of yourselves? As for my name!' He broke off, laughing long and softly. ...
'My name!' said the old man again. 'Have you not guessed it already? You have heard it before, I think. Yes, you have heard it before. But come now, what of your tale?'
The old man... stooped and sat himself on a low flat stone. Then his greay cloak drew apart, and they saw, beyond doubt, that he was clothed beneath all in white.
'Saruman!' cried Gimli, springing towards him with axe in hand. 'Speak! Tell us where you have hidden our friends! What have you done with them? Speak, or I will make a dint in your hat that even a wizard will find it hard to deal with!'
The old man was too quick for him. He sprang to his feet and leaped to the top of a large rock. There he stood, grown suddenly tall, towering above them. His hood and his grey rags were flung away. His white garments shone. He lifted up his staff, and Gimli's axe leaped from his grasp and fell ringing on the ground. The sword of Aragorn, stiff in his motionless hand, blazed with a sudden fire. Legolas gave a great shout and shot an arrow high into the air: it vanished in a flash of flame.
'Mithrandir!' he cried. 'Mithrandir!'
'Well met, I say to you again, Legolas!' said the old man.
They all gazed at him. His hair was white as snow in the sunshine; and gleaming white was his robe; the eyes under his deep brows were bright, piercing as the rays of the sun; power was in his hand. Between wonder, joy, and fear they stood and found no words to say.
At last Aragorn stirred. 'Gandalf!' he said. 'Beyond all hope you return to us in our need! What veil was over my sight? Gandalf!'
pp. 97-98, "The White Rider," The Two Towers by J.R.R. Tolkien
Aragorn's last line there could indicate that some sort of magic spell was involved in concealing Gandalf's identity from them, but this is never elaborated upon, at least not to my memory. As far as I can tell, they simply could not recognize him until after he removed his grey cloak, and even then, they initially mistook him for Saruman. Also, one of Gandalf's earlier quotes above indicates that he thought that they could easily ascertain his identity without him having to tell them.
Even if I allow for the possibility that Gandalf was deliberately concealing his identity in the book like he clearly was in the film, in either case, what would his purpose be for doing that?
It seems to me that in both cases, he clearly knew who they were, and that they were his allies.