Added LDAP migrations, made CreateObject async, moved LdapOptions to Models, fixed user password not hashed on user creation

This commit is contained in:
2025-10-03 16:31:08 +02:00
parent 45b1a44ebe
commit 1f356a807f
7 changed files with 220 additions and 23 deletions

View File

@@ -3,6 +3,8 @@ using Berufsschule_HAM.Services;
using Microsoft.AspNetCore.Mvc;
using Novell.Directory.Ldap;
using Berufsschule_HAM.Models;
using System.Security.Cryptography;
using System.Text;
[Route("[controller]")]
public class UsersController : Controller
@@ -58,7 +60,7 @@ public class UsersController : Controller
}
[HttpGet("Create")]
public bool Create(string cn, string sn, string? title, string? uid, string userPassword, string? description, string jpegPhoto)
public async Task<bool> Create(string cn, string sn, string? title, string? uid, string userPassword, string? description, string jpegPhoto)
{
try
{
@@ -66,8 +68,14 @@ public class UsersController : Controller
uid ??= sn.ToLower() + cn.ToLower();
title ??= "";
description ??= "{}";
LdapAttributeSet attributeSet = new LdapAttributeSet
if (!userPassword.StartsWith('{'))
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(userPassword);
byte[] hashedPassword = SHA256.HashData(passwordBytes);
userPassword = "{SHA256}" + Convert.ToBase64String(hashedPassword);
}
LdapAttributeSet attributeSet =
[
new LdapAttribute("objectClass", "inetOrgPerson"),
new LdapAttribute("cn", cn),
new LdapAttribute("sn", sn),
@@ -76,8 +84,8 @@ public class UsersController : Controller
new LdapAttribute("jpegPhoto", jpegPhoto),
new LdapAttribute("description", description),
new LdapAttribute("userPassword", userPassword)
};
_ldap.CreateUser(uid, attributeSet);
];
await _ldap.CreateUser(uid, attributeSet);
return true;
}
catch (Exception ex)