73 lines
2.8 KiB
C#
73 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.DirectoryServices;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Chrosey.Extensions
|
|
{
|
|
public static class ActiveDirectoryExtensions
|
|
{
|
|
public static T Get<T>(this DirectoryEntry entry, string propertyName) where T : class
|
|
{
|
|
if (string.IsNullOrWhiteSpace(propertyName)) throw new ArgumentNullException("propertyName darf nicht leer sein");
|
|
if (!entry.Properties.PropertyNames.Cast<string>().Contains(propertyName)) return null;
|
|
|
|
return entry.Properties[propertyName].Value as T;
|
|
}
|
|
|
|
public static string GetValue(this DirectoryEntry entry, string propertyName)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
|
|
if (null == entry) throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
|
|
if (!entry.Properties.Contains(propertyName)) return string.Empty;
|
|
|
|
return entry.Properties[propertyName].Value.ToString();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}",entry.Name,propertyName), e);
|
|
}
|
|
}
|
|
public static byte[] Photo(this DirectoryEntry entry)
|
|
{
|
|
try
|
|
{
|
|
if (null == entry) throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
|
|
if (!entry.Properties.Contains("thumbnailPhoto")) return null;
|
|
|
|
return entry.Properties["thumbnailPhoto"].Value as byte[];
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", entry.Name, "thumbnailPhoto"), e);
|
|
}
|
|
}
|
|
|
|
public static string GetValue(this SearchResult sr, string propertyName)
|
|
{
|
|
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
|
|
if (null == sr) throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
|
|
|
|
try
|
|
{
|
|
var s = sr.Properties[propertyName.ToLower()];
|
|
if (s.Count > 0)
|
|
{
|
|
return s[0].ToString();
|
|
}
|
|
return string.Empty;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", sr.Path, propertyName), e);
|
|
}
|
|
}
|
|
}
|
|
}
|