Esempio n. 1
0
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            ActiveDirectoryProfileProviderSection sec = ConfigurationManager.GetSection("activeDirectoryProvider") as ActiveDirectoryProfileProviderSection;


            //string connName = sec.ConnectionName;
            //connectionString  = ConfigurationManager.ConnectionStrings[connName].ConnectionString;
            //loginName = sec.LoginName;
            //loginPassword = sec.LoginPassword;
            //authType = sec.AuthenticationType;
            //...........

            //Process Attribute Mappings List
            IEnumerator en = sec.AttributeMappings.GetEnumerator();
            IDictionary <string, string> attrMappingsDict = new Dictionary <string, string>();
            IList <string> groupedAttributes = new List <string>();

            while (en.MoveNext())
            {
                AttributeMappingElement elem = (AttributeMappingElement)en.Current;
                attrMappingsDict[elem.ProfileAttribute] = elem.ActiveDirectoryUserAttribute;

                //A grouped property. So no recursion would be required
                if (elem.IsGroupedProperty)
                {
                    groupedAttributes.Add(elem.ProfileAttribute);
                }
            }

            //Recursively process the AttributeMappingList
            this.attributeList = CreateAttributeMappingList(attrMappingsDict, groupedAttributes);

            base.Initialize(name, config);
        }
Esempio n. 2
0
        private object GetPropertyRecursively(DirectoryEntry userEntry, string propertyToGet, Type destinationType,
                                              SettingsSerializeAs serializeAs, AttributeMappingList attributeListForContext)
        {
            //Get the Active Directory User attribute mapping
            AttributeMapping attrMap = attributeListForContext[propertyToGet];

            //if no such key exists i.e. attrMap is null, simply return null;

            //Primitives, string, simple collections and serialized complex types
            if (destinationType.IsPrimitive || destinationType.Equals(typeof(string)) ||
                typeof(ICollection).IsAssignableFrom(destinationType) || destinationType.IsArray || destinationType.IsValueType)
            {
                //Get value from Active Directory and convert it into correct type
                object convertedObject = GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, destinationType);

                return(convertedObject);
            }
            //Complex types
            else
            {
                if (serializeAs != SettingsSerializeAs.ProviderSpecific)
                {
                    if (serializeAs == SettingsSerializeAs.String || serializeAs == SettingsSerializeAs.Xml)
                    {
                        return(GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, typeof(string)));
                    }
                    else
                    {
                        return(GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, typeof(byte[])));
                    }
                }
                else
                {
                    //Get the mappings for the inner properties
                    AttributeMappingList innerMappings = attributeListForContext[propertyToGet].InnerMappingList;

                    //Create a new instance of the complex type using Activator.CreateInstance and default constructor.
                    object complexObj = Activator.CreateInstance(destinationType);

                    //Call AD to get value for each inner property
                    foreach (AttributeMapping innerMapping in innerMappings)
                    {
                        //Get the current property
                        PropertyInfo propInfo = destinationType.GetProperty(innerMapping.ProfileAttribute);

                        //Recursively call BuildComplexObject
                        object convertedObject = GetPropertyRecursively(userEntry, innerMapping.ProfileAttribute,
                                                                        propInfo.PropertyType, SettingsSerializeAs.ProviderSpecific, innerMappings);

                        //Set the property of complex type
                        propInfo.SetValue(complexObj, convertedObject, null);
                    }

                    return(complexObj);
                }
            }
        }
Esempio n. 3
0
        private void SetPropertyRecursively(DirectoryEntry userEntry, object propertyValue, bool isSerialized, AttributeMapping am)
        {
            Type propertyType = propertyValue.GetType();

            //Simple property
            if (am.InnerMappingList == null || am.InnerMappingList.Count == 0)
            {
                //Primitives, strings and serialized objects
                if (propertyType.IsPrimitive || propertyType.Equals(typeof(string)) || propertyType.IsValueType || isSerialized)
                {
                    //Set the value of the attribute. Uncomment this
                    //userEntry.InvokeSet(am.ActiveDirectoryUserAttribute, propertyValue);

                    return;
                }
                //Collections and arrays
                else if (typeof(ICollection).IsAssignableFrom(propertyType))
                {
                    ICollection coll = (ICollection)propertyValue;
                    object[]    arr  = new object[coll.Count];

                    //Populate the object array that will be saved.
                    int         i  = 0;
                    IEnumerator en = coll.GetEnumerator();
                    while (en.MoveNext())
                    {
                        arr[i] = en.Current;
                        i++;
                    }

                    //Save. Uncomment this
                    //userEntry.InvokeSet(am.ActiveDirectoryUserAttribute, arr);

                    return;
                }

                throw new Exception("Unknown format");
            }
            //Complex property
            else
            {
                AttributeMappingList innerMappingList = am.InnerMappingList;
                foreach (AttributeMapping innerAm in innerMappingList)
                {
                    SetPropertyRecursively(userEntry, propertyType.GetProperty(innerAm.ProfileAttribute).GetValue(propertyValue, null), false, innerAm);
                }
            }
        }
Esempio n. 4
0
        private AttributeMappingList CreateAttributeMappingList(IDictionary <string, string> attrMappingsDict, IList <string> groupedAttributes)
        {
            AttributeMappingList ret = new AttributeMappingList();

            //Key is prefix. Value is a genaral mapping dictionary similar to input of method
            Dictionary <string, IDictionary <string, string> > prefixedAttributes = new Dictionary <string, IDictionary <string, string> >();

            //Iterate dictionary
            IEnumerator <KeyValuePair <string, string> > en = attrMappingsDict.GetEnumerator();

            while (en.MoveNext())
            {
                //Complex object with nested properties using the '.' delimiter
                if (en.Current.Key.Contains(".") && !groupedAttributes.Contains(en.Current.Key))
                {
                    string prefix = en.Current.Key.Substring(0, en.Current.Key.IndexOf('.'));

                    //Is this first entry into prefix dictionary?
                    if (!prefixedAttributes.ContainsKey(prefix))
                    {
                        prefixedAttributes[prefix] = new Dictionary <string, string>();
                    }

                    prefixedAttributes[prefix].Add(en.Current.Key.Remove(0, prefix.Length + 1), en.Current.Value);
                }
                //Simple or grouped object
                else
                {
                    ret.Add(new AttributeMapping(en.Current.Key, en.Current.Value, null));
                }
            }

            //Now recursively process the prefixedAttributes dictionary.
            IEnumerator <KeyValuePair <string, IDictionary <string, string> > > enPrefix = prefixedAttributes.GetEnumerator();

            while (enPrefix.MoveNext())
            {
                //Recursively process the attribute mapping dictionary for current prefix
                AttributeMappingList listForPrefix = CreateAttributeMappingList(enPrefix.Current.Value, groupedAttributes);

                //Create an AttributeMapping with the innerMappingList set.
                ret.Add(new AttributeMapping(enPrefix.Current.Key, null, listForPrefix));
            }

            return(ret);
        }
Esempio n. 5
0
 public AttributeMapping(string profileAttribute, string activeDirectoryUserAttribute, AttributeMappingList innerMappingList)
 {
     this.activeDirectoryUserAttribute = activeDirectoryUserAttribute;
     this.profileAttribute             = profileAttribute;
     this.innerMappingList             = innerMappingList;
 }