Ejemplo n.º 1
0
        /// <summary>
        /// Attempts a login with the given credentials
        /// </summary>
        /// <param name="domainUser">The domain qualified user</param>
        /// <param name="credentials">The associated credentials</param>
        /// <param name="token"></param>
        /// <returns>True if the login was successful</returns>
        public async Task <bool> TryBindAsync(string domainUser, string credentials, CancellationToken token)
        {
            var op = new BindRequest {
                Name = domainUser, Authentication = new SimpleAuthentication {
                    Credentials = credentials
                }
            };

            foreach (var msg in await _connection.TryQueueOperation(op, token))
            {
                var res = msg as BindResponse;
                if (res != null && res.ResultCode == 0)
                {
                    _state = LDAPSessionState.Bound;
                    var dn = res.MatchedDN;
                    _current = new LDAPObject
                    {
                        DistinguishedName = dn,
                        Domain            = string.Join(",", dn.Split(',')
                                                        .Where(s => s.StartsWith("dc=", true, CultureInfo.InvariantCulture)).ToArray())
                    };
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Try to add a record to the directory
        /// </summary>
        /// <param name="obj">The LDAP Entity to add</param>
        /// <param name="add"></param>
        /// <param name="remove"></param>
        /// <param name="update"></param>
        /// <param name="token"></param>
        /// <returns>True if successful</returns>
        public async Task <LDAPResult> TryModify(LDAPObject obj, ICollection <LDAPAttribute> add, ICollection <LDAPAttribute> remove, ICollection <LDAPAttribute> update, CancellationToken token)
        {
            var op = new ModifyRequest {
                DistinguishedName = obj.DistinguishedName
            };

            if (add != null && add.Count > 0)
            {
                op.Added = add.ToArray();
            }

            if (remove != null && remove.Count > 0)
            {
                op.Removed = remove.ToArray();
            }

            if (update != null && update.Count > 0)
            {
                op.Modified = update.ToArray();
            }

            var objList = new List <LDAPObject>();
            var result  = new LDAPResult
            {
                Objects     = objList,
                IsStreaming = false,
            };

            foreach (var msg in await _connection.TryQueueOperation(op, token))
            {
                result.ResultCode    = (LDAPResultCode)msg.ResultCode;
                result.WasSuccessful = msg.ResultCode == 0;

                // Modify the attributes
                if (result.WasSuccessful)
                {
                    foreach (var attr in add ?? new List <LDAPAttribute>())
                    {
                        obj.Attributes.Add(attr);
                    }

                    foreach (var attr in update ?? new List <LDAPAttribute>())
                    {
                        obj.Attributes.RemoveAll((p) => p.Description.Equals(attr.Description));
                        obj.Attributes.Add(attr);
                    }

                    foreach (var attr in remove ?? new List <LDAPAttribute>())
                    {
                        obj.Attributes.RemoveAll((p) => p.Description.Equals(attr.Description));
                    }
                }

                objList.Add(obj);
                break;
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Clones the current object to another, identical entity
        /// </summary>
        /// <returns>A new copy of the object and all it's properties</returns>
        public LDAPObject Clone()
        {
            // Create clone of current
            var clone = new LDAPObject
            {
                DistinguishedName = DistinguishedName,
                Domain            = Domain,
            };

            // Force copy of objects (slow but prevents references)
            clone.Attributes.AddRange(Attributes.Select(a =>
            {
                var a1 = new LDAPAttribute {
                    Description = a.Description
                };
                a1.Values.AddRange(a.Values);

                return(a1);
            }));

            return(clone);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Try to remove a record from the directory
        /// </summary>
        /// <param name="obj">The entity to remove</param>
        /// <param name="token"></param>
        /// <returns>True if successful</returns>
        public async Task <LDAPResult> TryRemove(LDAPObject obj, CancellationToken token)
        {
            var op = new DeleteRequest {
                DistinguishedName = obj.DistinguishedName
            };
            var objList = new List <LDAPObject>();
            var result  = new LDAPResult
            {
                Objects     = objList,
                IsStreaming = false,
            };

            foreach (var msg in await _connection.TryQueueOperation(op, token))
            {
                result.ResultCode    = (LDAPResultCode)msg.ResultCode;
                result.WasSuccessful = msg.ResultCode == 0;
                objList.Add(obj);
                break;
            }

            return(result);
        }