by Guest » 20 Dec 2024, 14:47
using Microsoft.AspNetCore.Mvc;
using System.DirectoryServices;
using System.Security.Principal;
using Newtonsoft.Json;
namespace SDAdmConsole.Controllers;
[ApiController]
[Route("api/[controller]")]
public class SearchUsrController : Controller
{
public IActionResult Index()
{
// List of Domains to Check users
string[] domains = { "tst-ads-001", "tst-ads-001." };
// Active Directory Properties we need to bring the results for
string[] propertiesToLoad = { "samAccountName", "displayName", "objectSid", "department", "employeeNumber", "manager", "userPrincipalName","badPwdCount", "lockoutTime",
"lastLogon", "lastLogonTimestamp", "pwdLastSet", "homeDirectory", "extensionAttribute1", "distinguishedName", "userWorkstations"
};
// Master Credentials for Active Directory LDAP search
string domainUser = "redacted";
string domainPassword = "redacted";
// Filter used for Active Directory Search
string searchQuery = "(&(objectCategory=user)(objectClass=user)(samAccountName=*steven.test*))";
// New empty Dictionary to put all the results returned into.
List resultList = new List();
// Search each domain in the list of Domains
foreach (string domain in domains)
{
// New Search builder
DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + domain, domainUser, domainPassword), searchQuery, propertiesToLoad);
// Results returned as a collection
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
// Create Dictionary to store all the values from each user found
Dictionary resultDict = new Dictionary();
// Convert raw SID in to a readable value
byte[] sidBytes = (byte[])result.Properties["objectSid"][0];
SecurityIdentifier sid = new SecurityIdentifier(sidBytes, 0);
string sidString = sid.Value;
resultDict.Add("displayName", result.Properties["displayName"][0]);
resultDict.Add("sAMAccountName", result.Properties["sAMAccountName"][0]);
resultDict.Add("department", result.Properties["department"][0]);
resultDict.Add("employeeNumber", result.Properties["employeeNumber"][0]);
resultDict.Add("manager", result.Properties["manager"][0]);
resultDict.Add("userPrincipalName", result.Properties["userPrincipalName"][0]);
resultDict.Add("badPwdCount", result.Properties["badPwdCount"][0]);
resultDict.Add("lockoutTime", result.Properties["lockoutTime"][0]);
resultDict.Add("lastLogon", result.Properties["lastLogon"][0]);
resultDict.Add("lastLogonTimestamp", result.Properties["lastLogonTimestamp"][0]);
resultDict.Add("pwdLastSet", result.Properties["pwdLastSet"][0]);
resultDict.Add("homeDirectory", result.Properties["homeDirectory"][0]);
resultDict.Add("objectSid", sidString);
//resultDict.Add("extensionAttribute1", result.Properties["extensionAttribute1"][0]);
//resultDict.Add("distinguishedName", result.Properties["distinguishedName"][0]);
//resultDict.Add("userWorkstations", result.Properties["userWorkstations"][0]);
//resultDict.Add("extensionAttribute1", result.Properties["extensionAttribute1"][0]);
//resultDict.Add("mail", result.Properties["mail"][0]);
//resultDict.Add("memberof", result.Properties["memberof"][0]); */
resultList.Add(resultDict);
}
}
// Convert Dictionary in to JSON array
string json = JsonConvert.SerializeObject(resultList);
// Return JSON List for the API request
return Ok(json);
}
}
Die API funktioniert hervorragend, wenn alle zurückgegebenen Eigenschaften Werte enthalten. Wenn einer von ihnen einen leeren Wert hat, beispielsweise keinen Home-Ordner, schlägt die API-Anfrage fehl und gibt stattdessen den Homepage-HTML-Code zurück. Beim Kompilieren treten keine Fehler auf, daher weiß ich nicht, was ich sonst noch versuchen soll.
Ich habe eine if- und else-Anweisung ausprobiert, um anzugeben, ob der Wert leer oder null ist, und dann diese Zeichenfolge einzufügen schlägt aber immer noch fehl.
Jeder Rat wäre toll, danke
using Microsoft.AspNetCore.Mvc;
using System.DirectoryServices;
using System.Security.Principal;
using Newtonsoft.Json;
namespace SDAdmConsole.Controllers;
[ApiController]
[Route("api/[controller]")]
public class SearchUsrController : Controller
{
public IActionResult Index()
{
// List of Domains to Check users
string[] domains = { "tst-ads-001", "tst-ads-001." };
// Active Directory Properties we need to bring the results for
string[] propertiesToLoad = { "samAccountName", "displayName", "objectSid", "department", "employeeNumber", "manager", "userPrincipalName","badPwdCount", "lockoutTime",
"lastLogon", "lastLogonTimestamp", "pwdLastSet", "homeDirectory", "extensionAttribute1", "distinguishedName", "userWorkstations"
};
// Master Credentials for Active Directory LDAP search
string domainUser = "redacted";
string domainPassword = "redacted";
// Filter used for Active Directory Search
string searchQuery = "(&(objectCategory=user)(objectClass=user)(samAccountName=*steven.test*))";
// New empty Dictionary to put all the results returned into.
List resultList = new List();
// Search each domain in the list of Domains
foreach (string domain in domains)
{
// New Search builder
DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + domain, domainUser, domainPassword), searchQuery, propertiesToLoad);
// Results returned as a collection
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
// Create Dictionary to store all the values from each user found
Dictionary resultDict = new Dictionary();
// Convert raw SID in to a readable value
byte[] sidBytes = (byte[])result.Properties["objectSid"][0];
SecurityIdentifier sid = new SecurityIdentifier(sidBytes, 0);
string sidString = sid.Value;
resultDict.Add("displayName", result.Properties["displayName"][0]);
resultDict.Add("sAMAccountName", result.Properties["sAMAccountName"][0]);
resultDict.Add("department", result.Properties["department"][0]);
resultDict.Add("employeeNumber", result.Properties["employeeNumber"][0]);
resultDict.Add("manager", result.Properties["manager"][0]);
resultDict.Add("userPrincipalName", result.Properties["userPrincipalName"][0]);
resultDict.Add("badPwdCount", result.Properties["badPwdCount"][0]);
resultDict.Add("lockoutTime", result.Properties["lockoutTime"][0]);
resultDict.Add("lastLogon", result.Properties["lastLogon"][0]);
resultDict.Add("lastLogonTimestamp", result.Properties["lastLogonTimestamp"][0]);
resultDict.Add("pwdLastSet", result.Properties["pwdLastSet"][0]);
resultDict.Add("homeDirectory", result.Properties["homeDirectory"][0]);
resultDict.Add("objectSid", sidString);
//resultDict.Add("extensionAttribute1", result.Properties["extensionAttribute1"][0]);
//resultDict.Add("distinguishedName", result.Properties["distinguishedName"][0]);
//resultDict.Add("userWorkstations", result.Properties["userWorkstations"][0]);
//resultDict.Add("extensionAttribute1", result.Properties["extensionAttribute1"][0]);
//resultDict.Add("mail", result.Properties["mail"][0]);
//resultDict.Add("memberof", result.Properties["memberof"][0]); */
resultList.Add(resultDict);
}
}
// Convert Dictionary in to JSON array
string json = JsonConvert.SerializeObject(resultList);
// Return JSON List for the API request
return Ok(json);
}
}
Die API funktioniert hervorragend, wenn alle zurückgegebenen Eigenschaften Werte enthalten. Wenn einer von ihnen einen leeren Wert hat, beispielsweise keinen Home-Ordner, schlägt die API-Anfrage fehl und gibt stattdessen den Homepage-HTML-Code zurück. Beim Kompilieren treten keine Fehler auf, daher weiß ich nicht, was ich sonst noch versuchen soll.
Ich habe eine if- und else-Anweisung ausprobiert, um anzugeben, ob der Wert leer oder null ist, und dann diese Zeichenfolge einzufügen schlägt aber immer noch fehl.
Jeder Rat wäre toll, danke