/// <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); }
/// <summary> /// Ends the session and closes all resources asynchronously /// </summary> /// <remarks> /// Note that any outstanding requests will be abandoned and may not finish appropriately /// and certain states will require this operation to wait for completion before stopping /// </remarks> public async Task CloseAsync() { await _connection.CloseAsync(); Dispose(); _state = LDAPSessionState.Closed; }
/// <summary> /// Default constructor /// </summary> /// <param name="options">Options for LDAP communication</param> /// <param name="log"></param> public LDAPSession(IOptions <LDAPConfiguration> options, ILogger <LDAPSession> log) { // Sessions start closed _state = LDAPSessionState.Closed; _options = options.Value; _connection = new LDAPConnection(_options.IsSecured, log); _log = log; }
/// <summary> /// Starts the session asynchronously /// </summary> public async Task StartAsync() { // Only start the connection if not initialized if (_state == LDAPSessionState.Closed || _connection.State == LDAPConnectionState.NotInitialized) { await _connection.ConnectAsync(_options.Host, _options.Port); } _state = LDAPSessionState.Open; }