Files
Berufsschule_HAM/Controllers/UsersController.cs
2025-09-26 11:40:52 +02:00

63 lines
1.8 KiB
C#

// Controllers/UsersController.cs
using Berufsschule_HAM.Services;
using Microsoft.AspNetCore.Mvc;
using Novell.Directory.Ldap;
using System.Threading.Tasks;
[Route("[controller]")]
public class UsersController : Controller
{
private readonly LdapService _ldap;
public UsersController(LdapService ldap) => _ldap = ldap;
[HttpGet("Index")]
public async Task<IEnumerable<Dictionary<string, string>>> Index()
{
var users = await _ldap.ListUsersAsync();
return users;
}
[HttpGet("Delete")]
public async Task<bool> Delete(string dn)
{
return await Task.Run(() =>
{
try
{
_ldap.DeleteObjectByDn(dn);
return true;
}
catch (Exception)
{
return false;
}
});
}
[HttpGet("Create")]
public async Task<bool> Create(string cn, string sn, string userPassword)
{
// return await new Task<bool>(() =>
// {
try
{
LdapAttributeSet attributeSet = [];
attributeSet.Add(new LdapAttribute("objectClass", "organizationalPerson"));
attributeSet.Add(new LdapAttribute("objectClass", "person"));
//attributeSet.Add(new LdapAttribute("ou", "users"));
attributeSet.Add(new LdapAttribute("objectClass", "top"));
attributeSet.Add(new LdapAttribute("cn", cn));
attributeSet.Add(new LdapAttribute("sn", sn));
attributeSet.Add(new LdapAttribute("userPassword", userPassword));
_ldap.CreateUser(attributeSet);
return true;
}
catch (Exception ex)
{
return false;
}
// });
}
}