A Mg(ClO4)2-catalyzed intramolecular amination of allylic alcohols with carbamate or sulfonamide nucleophiles to form substituted piperidine and pyrrolidine derivatives has been developed. This method has been successfully applied to the total synthesis of demethoxyfumitremorgin C.