Sahih Muslim Book 20, Hadith Number 4541.

Chapter : Obedience to the ruler is forbidden in matters sinful, but is otherwise obligatory.

It has been narrated on the authority of Junida b. Abu Umayya who said: We called upon ‘Ubada b. Samit who was ill and said to him: May God give you health I Narrate to us a tradition which God may prove beneficial (to us) and which you have heard from the Messenger of Allah (may peace be upon him). He said: The Messenger of Allah (may peace be upon him) called us and we took the oath of allegiance to him. Among the injunctions he made binding upon us was: Listening and obedience (to the Amir) in our pleasure and displeasure, in our adversity and prosperity, even when somebody is given preference over us, and without disputing the delegation of powers to a man duly invested with them (Obedience shall be accorded to him in all circumstances) except when you have clear signs of his disbelief in (or disobedience to) God-signs that could be used as a conscientious justification (for non-compliance with his orders).