Getting the UDID this way is possible through Apple's Over-the-Air Profile Delivery Concepts
.
Read more
It is definitely not secure. The profile you are installing could be infected with malware and point your device to malicious servers and install malicious certificates (read more). Also your device information including UDID could be taken and potentially result in privacy violation.
If the configuration profile is removed, the harmful changes will be erased.