Soldiers take an oath to the constitution, not the president, so all members of the military have the right, and in some cases have the duty, to refuse illegal orders. Source.
If the law disagrees, the law is wrong and should be broken. And if that leads to a punishment, just remember that someone is being ordered to punish you, and they can defy those orders too.