112 lines
3.5 KiB
C#
112 lines
3.5 KiB
C#
namespace LaaProduction.Console
|
|
{
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.DirectoryServices;
|
|
using System.DirectoryServices.AccountManagement;
|
|
using System.Linq;
|
|
|
|
public static class Program
|
|
{
|
|
public static void Main()
|
|
{
|
|
var software = Software<Software>();
|
|
|
|
Console.WriteLine(JsonConvert.SerializeObject(software, Formatting.Indented));
|
|
|
|
Console.Write("Press any key for exit: ");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
public static Software Software<T>()
|
|
{
|
|
var assembly = typeof(T).Assembly.GetName();
|
|
|
|
return new Software
|
|
{
|
|
Assembly = assembly.Name,
|
|
Version = assembly.Version.ToString(),
|
|
CurrentUser = SoftwareUser()
|
|
};
|
|
}
|
|
|
|
public static SoftwareUser SoftwareUser()
|
|
{
|
|
var softwareUser = new SoftwareUser();
|
|
var userPrincipal = UserPrincipal.Current;
|
|
|
|
userPrincipal.DistinguishedName.ParseSearchResultProperties(softwareUser.Values);
|
|
|
|
softwareUser.Values = softwareUser.Values.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
|
|
if (softwareUser.Values.TryGetValue("manager", out string adspath))
|
|
{
|
|
softwareUser.Manager = new SoftwareUser();
|
|
|
|
adspath.ParseSearchResultProperties(softwareUser.Manager.Values);
|
|
|
|
softwareUser.Manager.Values = softwareUser.Manager.Values.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
|
|
}
|
|
|
|
return softwareUser;
|
|
}
|
|
|
|
private static void ParseSearchResultProperties(this string absPath, IDictionary<string, string> values)
|
|
{
|
|
try
|
|
{
|
|
var exclude = new[] { "comment", "userparameters" };
|
|
|
|
using (var directoryEntry = new DirectoryEntry($"LDAP://{absPath}"))
|
|
{
|
|
using (var directorySearcher = new DirectorySearcher(directoryEntry))
|
|
{
|
|
var searchResult = directorySearcher.FindOne();
|
|
|
|
foreach (DictionaryEntry dictionaryEntry in searchResult.Properties)
|
|
{
|
|
if (dictionaryEntry.Key is string property && !exclude.Contains(property))
|
|
{
|
|
if (dictionaryEntry.Value is ResultPropertyValueCollection dictionaryEntryValues)
|
|
{
|
|
if (dictionaryEntryValues.Count > 0 && dictionaryEntryValues[0] is string value)
|
|
{
|
|
values[property] = value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
values[nameof(e.Message)] = e.Message;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class Software
|
|
{
|
|
public string Assembly { get; set; }
|
|
|
|
public string Version { get; set; }
|
|
|
|
public SoftwareUser CurrentUser { get; set; }
|
|
}
|
|
|
|
public class SoftwareUser
|
|
{
|
|
public SoftwareUser()
|
|
=> this.Values = new Dictionary<string, string>();
|
|
|
|
public SoftwareUser Manager { get; set; }
|
|
|
|
public IDictionary<string, string> Values { get; set; }
|
|
}
|
|
}
|