forked from UniStuttgart-VISUS/Visus.LdapAuthentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LdapConnectionService.cs
74 lines (64 loc) · 2.52 KB
/
LdapConnectionService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// <copyright file="LdapConnectionService.cs" company="Visualisierungsinstitut der Universität Stuttgart">
// Copyright © 2021 - 2024 Visualisierungsinstitut der Universität Stuttgart.
// Licensed under the MIT licence. See LICENCE file for details.
// </copyright>
// <author>Christoph Müller</author>
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.DirectoryServices.Protocols;
namespace Visus.DirectoryAuthentication {
/// <summary>
/// Generic LDAP connection service using the server and credentials
/// specified in <see cref="ILdapOptions"/>.
/// </summary>
/// <remarks>
/// Use this service if you need access to the LDAP directory to compute any
/// special claims that cannot be derived via the user classes provided by
/// the library.
/// </remarks>
public sealed class LdapConnectionService : ILdapConnectionService {
#region Public constructors
/// <summary>
/// Initialises a new instance.
/// </summary>
/// <param name="options"></param>
/// <param name="logger"></param>
public LdapConnectionService(ILdapOptions options,
ILogger<LdapConnectionService> logger) {
this._logger = logger
?? throw new ArgumentNullException(nameof(logger));
this.Options = options
?? throw new ArgumentNullException(nameof(options));
}
/// <summary>
/// Initialises a new instance.
/// </summary>
/// <param name="options"></param>
/// <param name="logger"></param>
public LdapConnectionService(IOptions<LdapOptions> options,
ILogger<LdapConnectionService> logger)
: this(options?.Value, logger) { }
#endregion
#region Public properties
/// <inheritdoc />
public ILdapOptions Options { get; }
#endregion
#region Public methods
/// <inheritdoc />
public LdapConnection Connect() {
// TODO: Add the option to connect with negotiate on Windows.
return this.Options.Connect(this.Options.User,
this.Options.Password,
this._logger);
}
/// <inheritdoc />
public LdapConnection Connect(string username, string password) {
return this.Options.Connect(username, password, this._logger);
}
#endregion
#region Private fields
private readonly ILogger _logger;
#endregion
}
}