internal void Copy(IdentityElement source)
 {
     if (source == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("source");
     }
     PropertyInformationCollection properties = source.ElementInformation.Properties;
     if (properties["userPrincipalName"].ValueOrigin != PropertyValueOrigin.Default)
     {
         this.UserPrincipalName.Value = source.UserPrincipalName.Value;
     }
     if (properties["servicePrincipalName"].ValueOrigin != PropertyValueOrigin.Default)
     {
         this.ServicePrincipalName.Value = source.ServicePrincipalName.Value;
     }
     if (properties["certificate"].ValueOrigin != PropertyValueOrigin.Default)
     {
         this.Certificate.EncodedValue = source.Certificate.EncodedValue;
     }
     if (properties["certificateReference"].ValueOrigin != PropertyValueOrigin.Default)
     {
         this.CertificateReference.StoreName = source.CertificateReference.StoreName;
         this.CertificateReference.StoreLocation = source.CertificateReference.StoreLocation;
         this.CertificateReference.X509FindType = source.CertificateReference.X509FindType;
         this.CertificateReference.FindValue = source.CertificateReference.FindValue;
     }
 }
        /// <summary>
        /// Gets the identity.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>EndpointIdentity.</returns>
        /// <exception cref="System.InvalidOperationException">UnableToLoadCertificateIdentity</exception>
        private static EndpointIdentity GetIdentity(IdentityElement element)
        {
            var properties = element.ElementInformation.Properties;

            var userPrincipalName = properties["userPrincipalName"];
            if (userPrincipalName != null && userPrincipalName.ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateUpnIdentity(element.UserPrincipalName.Value);
            }

            var servicePrincipalName = properties["servicePrincipalName"];
            if (servicePrincipalName != null && servicePrincipalName.ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateSpnIdentity(element.ServicePrincipalName.Value);
            }

            var dns = properties["dns"];
            if (dns != null && dns.ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateDnsIdentity(element.Dns.Value);
            }

            var rsa = properties["rsa"];
            if (rsa != null && rsa.ValueOrigin != PropertyValueOrigin.Default)
            {
                return EndpointIdentity.CreateRsaIdentity(element.Rsa.Value);
            }

            var certificate = properties["certificate"];
            if (certificate != null && certificate.ValueOrigin != PropertyValueOrigin.Default)
            {
                var supportingCertificates = new X509Certificate2Collection();
                supportingCertificates.Import(Convert.FromBase64String(element.Certificate.EncodedValue));
                if (supportingCertificates.Count == 0)
                {
                    throw new InvalidOperationException("UnableToLoadCertificateIdentity");
                }

                var primaryCertificate = supportingCertificates[0];
                supportingCertificates.RemoveAt(0);
                return EndpointIdentity.CreateX509CertificateIdentity(primaryCertificate, supportingCertificates);
            }

            return null;
        }