This question already has an answer here:
Sam - Near Crith Ungol:
Sam puts on ring and " Sam hurled himself against the bolted brazen plates and fell senseless to the ground. He was out in the darkness"
He put the ring on before he goes unconscious. He woke up with the ring and The Dark Lord did not detect him.
Already the Ring tempted him, gnawing at his will and reason. Wild fantasies arose in his mind; and he saw Samwise the Strong, Hero of the Age, striding with a flaming sword across the darkened land, and armies flocking to his call as he marched to the overthrow of Barad-dûr. And then all the clouds rolled away, and the white sun shone, and at his command the vale of Gorgoroth became a garden of flowers and trees and brought forth fruit. He had only to put on the Ring and claim it for his own, and all this could be.
Frodo - At Mount Doom:
‘I have come,’ he said. ‘But I do not choose now to do what I came to do. I will not do this deed. The Ring is mine!’ And suddenly, as he set it on his finger, he vanished from Sam’s sight. Sam gasped, but he had no chance to cry out, for at that moment many things happened.
Something struck Sam violently in the back, his legs were knocked from under him and he was flung aside, striking his head against the stony floor, as a dark shape sprang over him. He lay still and for a moment all went black.
And far away, as Frodo put on the Ring and claimed it for his own, even in Sammath Naur the very heart of his realm, the Power in Barad-dûr was shaken, and the Tower trembled from its foundations to its proud and bitter crown. The Dark Lord was suddenly aware of him...