Browse Source

Use LdapForNet instead of Novell.

Make Ldap async!
pull/6368/head
maliming 5 years ago
parent
commit
e91dd06f3e
  1. 2
      framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj
  2. 6
      framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/ILdapManager.cs
  3. 34
      framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs
  4. 10
      framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapManager_Tests.cs

2
framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj

@ -15,7 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="3.2.0" />
<PackageReference Include="LdapForNet" Version="2.7.11" />
</ItemGroup>
<ItemGroup>

6
framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/ILdapManager.cs

@ -1,7 +1,9 @@
namespace Volo.Abp.Ldap
using System.Threading.Tasks;
namespace Volo.Abp.Ldap
{
public interface ILdapManager
{
bool Authenticate(string username, string password);
Task<bool> AuthenticateAsync(string username, string password);
}
}

34
framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs

@ -1,6 +1,8 @@
using System;
using System.Threading.Tasks;
using LdapForNet;
using LdapForNet.Native;
using Microsoft.Extensions.Options;
using Novell.Directory.Ldap;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
@ -15,17 +17,16 @@ namespace Volo.Abp.Ldap
public LdapManager(IOptions<AbpLdapOptions> ldapSettingsOptions)
{
LdapOptions = ldapSettingsOptions.Value;
Logger = NullLogger<LdapManager>.Instance;
}
public bool Authenticate(string username, string password)
public virtual async Task<bool> AuthenticateAsync(string username, string password)
{
try
{
using (var conn = CreateLdapConnection())
using (var conn = await CreateLdapConnectionAsync())
{
AuthenticateLdapConnection(conn, username, password);
await AuthenticateLdapConnectionAsync(conn, username, password);
return true;
}
}
@ -36,22 +37,33 @@ namespace Volo.Abp.Ldap
}
}
protected virtual ILdapConnection CreateLdapConnection()
protected virtual async Task<ILdapConnection> CreateLdapConnectionAsync()
{
var ldapConnection = new LdapConnection();
ConfigureLdapConnection(ldapConnection);
ldapConnection.Connect(LdapOptions.ServerHost, LdapOptions.ServerPort);
await ConfigureLdapConnectionAsync(ldapConnection);
await ConnectAsync(ldapConnection);
return ldapConnection;
}
protected virtual void ConfigureLdapConnection(ILdapConnection connection)
protected virtual Task ConfigureLdapConnectionAsync(ILdapConnection ldapConnection)
{
return Task.CompletedTask;
}
protected virtual Task ConnectAsync(ILdapConnection ldapConnection)
{
ldapConnection.Connect(LdapOptions.ServerHost, LdapOptions.ServerPort);
return Task.CompletedTask;
}
protected virtual void AuthenticateLdapConnection(ILdapConnection connection, string username, string password)
protected virtual async Task AuthenticateLdapConnectionAsync(ILdapConnection connection, string username, string password)
{
connection.Bind(username, password);
await connection.BindAsync(Native.LdapAuthType.Simple, new LdapCredential()
{
UserName = username,
Password = password
});
}
}
}

10
framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapManager_Tests.cs

@ -1,4 +1,5 @@
using Shouldly;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Testing;
using Xunit;
@ -19,10 +20,11 @@ namespace Volo.Abp.Ldap
}
[Fact(Skip = "Required Ldap environment")]
public void Authenticate()
public async Task AuthenticateAsync()
{
_ldapManager.Authenticate("cn=abp,dc=abp,dc=io", "123qwe").ShouldBe(true);
_ldapManager.Authenticate("NoExists", "123qwe").ShouldBe(false);
(await _ldapManager.AuthenticateAsync("cn=abp,dc=abp,dc=io", "123qwe")).ShouldBe(true);
(await _ldapManager.AuthenticateAsync("cn=abp,dc=abp,dc=io", "123123")).ShouldBe(false);
(await _ldapManager.AuthenticateAsync("NoExists", "123qwe")).ShouldBe(false);
}
}
}

Loading…
Cancel
Save