They can ask, however is it legal?, surely outside of work they have no right
Why would asking you not to talk to somebody be illegal? There is a difference between being asked to do or not do something and being obliged or contracted to do it not do something.