Sahih Muslim Book 20, Hadith Number 4538.

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

It has been narrated on the authority of “Ubida who learnt the tradition from his father who, in turn, learnt it from his own father. ‘Ubada’s grandfather said: The Messenger of Allah (may peace be upon him) took an oath of allegiance from us on our listening to and obeying the orders of our commander in adversity and prosperity, in pleasure and displeasure (and even) when somebody is given preference over us, on our avoiding to dispute the delegation of powers to a person deemed to be a fit recipient thereof (in the eye of one who delegates it) and on our telling the truth in whatever position we be without fearing in the matter of Allah the reproach of the reproacher.