Example #1
0
        /// <summary>
        /// Get AttributeSet for OpenLDAP
        /// </summary>
        /// <param name="entry">OpenLdapUserEntry object</param>
        /// <returns>LdapAttributeSet</returns>
        private async Task <LdapAttributeSet> getAttrSetForOpenLdapAsync(OpenLdapUserEntry entry)
        {
            #region Required attributes

            var attrSet = new LdapAttributeSet()
            {
                new LdapAttribute("objectClass", "inetOrgPerson"),
                new LdapAttribute("displayName", entry.DisplayName),
                new LdapAttribute("uid", entry.Uid),
                new LdapAttribute("sn", entry.SecondName),
                new LdapAttribute("mail", entry.Email),
                new LdapAttribute("userPassword", this.hashPwdAsync(entry.Pwd).Result)
            };
            #endregion

            #region Optional attributes
            // Notice that the value cannot be null!

            if (!string.IsNullOrEmpty(entry.FirstName))
            {
                attrSet.Add(new LdapAttribute("givenName", entry.FirstName));
            }
            #endregion

            return(await Task.FromResult(attrSet));
        }
        public async Task <IActionResult> Update(LdapUserEntry entry)
        {
            var ldapUser = new OpenLdapUserEntry(
                entry.UserName, entry.Password, entry.Email, entry.DisplayName, entry.FirstName, entry.SecondName);

            if (await this.ldapUserMgr.UpdateAsync(ldapUser))
            {
                return(this.Ok());
            }
            else
            {
                return(this.BadRequest());
            }
        }
        public async Task <IActionResult> Create(LdapUserEntry entry)
        {
            var ldapUser = new OpenLdapUserEntry(
                entry.UserName, entry.Password, entry.Email, entry.DisplayName, entry.FirstName, entry.SecondName);

            if (await this.ldapUserMgr.CreateAsync(ldapUser))
            {
                return(this.StatusCode(StatusCodes.Status201Created));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status409Conflict));
            }
        }
Example #4
0
        /// <summary>
        /// Add a new LDAP user
        /// </summary>
        /// <param name="entry">OpenLdapUserEntry object</param>
        /// <returns>True(Success)/False(Fail)</returns>
        public async Task <bool> CreateAsync(OpenLdapUserEntry entry)
        {
            Func <LdapConnection, bool> action = (ldapConn) =>
            {
                if (this.FindAsync(entry.Uid).Result == null)
                {
                    var    attributeSet = this.getAttrSetForOpenLdapAsync(entry).Result;
                    string userDn       = this.getUserDefaultDnAsync(entry.Uid).Result;
                    ldapConn.Add(new LdapEntry(userDn, attributeSet));
                    return(true);
                }
                else
                {
                    return(false);
                }
            };

            return(await this.ldapActionAsync(action));
        }
Example #5
0
        /// <summary>
        /// Add a new LDAP user
        /// </summary>
        /// <param name="entry">OpenLdapUserEntry object</param>
        /// <returns>True(Success)/False(Fail)</returns>
        public async Task <bool> UpdateAsync(OpenLdapUserEntry entry)
        {
            Func <LdapConnection, bool> action = (ldapConn) =>
            {
                var existEntry = this.FindAsync(entry.Uid).Result;
                if (existEntry != null)
                {
                    var modifiedAttributes = new ArrayList();

                    // Iterate all properties and add to modifiedAttributes
                    PropertyInfo[] props = typeof(OpenLdapUserEntry).GetProperties();
                    foreach (PropertyInfo prop in props)
                    {
                        var ldapAttr = Attribute.GetCustomAttributes(prop).FirstOrDefault(a => a.GetType().Equals(typeof(LdapAttrAttribute))) as LdapAttrAttribute;

                        if (ldapAttr != null)
                        {
                            var name  = ldapAttr.Name;
                            var value = prop.GetValue(entry, null)?.ToString();

                            if (!string.IsNullOrEmpty(value))
                            {
                                modifiedAttributes.Add(new LdapModification(LdapModification.REPLACE, new LdapAttribute(name, value)));
                            }
                        }
                    }

                    var ldapModification = new LdapModification[modifiedAttributes.Count];
                    ldapModification = (LdapModification[])modifiedAttributes.ToArray(typeof(LdapModification));

                    ldapConn.Modify(existEntry.DN, ldapModification);
                    return(true);
                }
                else
                {
                    return(false);
                }
            };

            return(await this.ldapActionAsync(action));
        }