[TASK] update to latest dll

This commit is contained in:
TLRZ Seyfferth 2020-04-08 18:40:10 +02:00
parent c94e357006
commit 980257a2d0
9 changed files with 394 additions and 45 deletions

View File

@ -1,4 +1,6 @@
using System;
using Chrosey.Extensions.Interfaces;
using Chrosey.Extensions.Wrapper;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
@ -11,62 +13,161 @@ namespace Chrosey.Extensions
{
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;
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (entry == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
return !entry.Properties.Contains(propertyName) ? default(T) : entry.Properties[propertyName].Value as T;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)entry.Name, (object)propertyName), ex);
}
}
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();
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (entry == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
return !entry.Properties.Contains(propertyName) ? string.Empty : entry.Properties[propertyName].Value.ToString();
}
catch (Exception e)
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}",entry.Name,propertyName), e);
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)entry.Name, (object)propertyName), ex);
}
}
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[];
if (entry == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
return !entry.Properties.Contains("thumbnailPhoto") ? (byte[])null : entry.Properties["thumbnailPhoto"].Value as byte[];
}
catch (Exception e)
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", entry.Name, "thumbnailPhoto"), e);
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)entry.Name, (object)"thumbnailPhoto"), ex);
}
}
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)
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (sr == null)
throw new NullReferenceException("Kann keine Eigenschaft von null abrufen");
ResultPropertyValueCollection property = sr.Properties[propertyName.ToLower()];
return property.Count > 0 ? property[0].ToString() : string.Empty;
}
catch (Exception ex)
{
return s[0].ToString();
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)sr.Path, (object)propertyName), ex);
}
return string.Empty;
}
catch (Exception e)
public static IEnumerable<T> GetList<T>(
this SearchResult source,
string propertyName)
where T : class
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", sr.Path, propertyName), e);
}
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (source == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
if (!source.Properties.Contains(propertyName))
return (IEnumerable<T>)null;
ResultPropertyValueCollection property = source.Properties[propertyName.ToLower()];
return property.Count > 0 ? (IEnumerable<T>)property.Cast<T>().ToList<T>() : (IEnumerable<T>)null;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex);
}
}
public static IEnumerable<T> GetList<T>(
this DirectoryEntry source,
string propertyName)
where T : class
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (source == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
if (!source.Properties.Contains(propertyName))
return (IEnumerable<T>)null;
PropertyValueCollection property = source.Properties[propertyName];
return property.Count > 0 ? (IEnumerable<T>)property.Cast<T>().ToList<T>() : (IEnumerable<T>)null;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex);
}
}
public static T Get<T>(this SearchResult source, string propertyName) where T : class
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (source == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
if (!source.Properties.Contains(propertyName))
return default(T);
ResultPropertyValueCollection property = source.Properties[propertyName.ToLower()];
return property.Count > 0 ? property[0] as T : default(T);
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex);
}
}
public static T Make<T>(this SearchResult source) where T : ISearchResultWrapper
{
T instance = Activator.CreateInstance<T>();
instance.Bind(source);
return instance;
}
public static IEnumerable<T> Make<T>(this SearchResultCollection source) where T : ISearchResultWrapper
{
List<T> objList = new List<T>();
foreach (SearchResult source1 in source)
objList.Add(source1.Make<T>());
return (IEnumerable<T>)objList;
}
public static T FindAs<T>(this DirectorySearcher searcher, IEnumerable<string> filter) where T : ASearchResultWrapper
{
string str = "(&(" + string.Join(")(", filter) + "))";
IEnumerable<string> source = searcher.PropertiesToLoad.Cast<string>();
string filter1 = searcher.Filter;
T instance = Activator.CreateInstance<T>();
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.AddRange(instance.propertiesToLoad);
searcher.Filter = str;
SearchResult one = searcher.FindOne();
if (one == null)
throw new NotFoundException("Element wurde nicht gefunden");
searcher.Filter = filter1;
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.AddRange(source.ToArray<string>());
return one.Make<T>();
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -48,14 +48,19 @@
<Compile Include="ActiveDirectoryExtensions.cs" />
<Compile Include="BasicExtensions.cs" />
<Compile Include="CollectionExtensions.cs" />
<Compile Include="Helper\PasswordGenerator.cs" />
<Compile Include="Helper\SemiNumericComparer.cs" />
<Compile Include="ImageExtensions.cs" />
<Compile Include="Interfaces\ISearchResultWrapper.cs" />
<Compile Include="Exceptions\NotFoundException.cs" />
<Compile Include="NumberExtensions.cs" />
<Compile Include="ColorExtensions.cs" />
<Compile Include="PredicateBuilder.cs" />
<Compile Include="RandomSecureVersion.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Wrapper\ConcurrentQueueWrapper.cs" />
<Compile Include="Wrapper\ASearchResultWrapper.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -1,4 +1,5 @@
using System;
using Chrosey.Extensions.Helper;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
@ -9,16 +10,12 @@ namespace Chrosey.Extensions
{
public static class CollectionExtensions
{
private static readonly Lazy<RandomSecureVersion> RandomSecure = new Lazy<RandomSecureVersion>((Func<RandomSecureVersion>)(() => new RandomSecureVersion()));
public static T Get<T>(this Dictionary<string, T> dict, string key) where T : class
{
T value;
if (!dict.TryGetValue(key,out value))
{
return null;
T obj;
return !dict.TryGetValue(key, out obj) ? default(T) : obj;
}
return value;
}
public static IEnumerable<T> Filter<T>(this IEnumerable<T> collection, NameValueCollection query) where T : class
{
@ -139,5 +136,21 @@ namespace Chrosey.Extensions
{
return collection.Filter(query).Sort(query, defaultSortProperty).GroupBy(query);
}
public static string ToQueryString(this IEnumerable<KeyValuePair<string, string>> me)
{
return string.Join("&", me.Select<KeyValuePair<string, string>, string>((Func<KeyValuePair<string, string>, string>)(x => x.Key + "=" + x.Value)));
}
public static IEnumerable<T> ShuffleSecure<T>(this IEnumerable<T> source)
{
T[] sourceArray = source.ToArray<T>();
for (int counter = 0; counter < sourceArray.Length; ++counter)
{
int randomIndex = CollectionExtensions.RandomSecure.Value.Next(counter, sourceArray.Length);
yield return sourceArray[randomIndex];
sourceArray[randomIndex] = sourceArray[counter];
}
}
}
}

107
Helper/PasswordGenerator.cs Normal file
View File

@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Chrosey.Extensions.Helper
{
public class PasswordGenerator
{
private readonly RandomSecureVersion _randomSecure = new RandomSecureVersion();
private readonly string _allAvailableChars;
private int _minimumNumberOfChars;
public int MinimumLengthPassword { get; private set; }
public int MaximumLengthPassword { get; private set; }
public int MinimumLowerCaseChars { get; private set; }
public int MinimumUpperCaseChars { get; private set; }
public int MinimumNumericChars { get; private set; }
public int MinimumSpecialChars { get; private set; }
public static string AllLowerCaseChars { get; private set; }
public static string AllUpperCaseChars { get; private set; }
public static string AllNumericChars { get; private set; }
public static string AllSpecialChars { get; private set; }
static PasswordGenerator()
{
PasswordGenerator.AllLowerCaseChars = PasswordGenerator.GetCharRange('a', 'z', "");
PasswordGenerator.AllUpperCaseChars = PasswordGenerator.GetCharRange('A', 'Z', "");
PasswordGenerator.AllNumericChars = PasswordGenerator.GetCharRange('0', '9', "");
PasswordGenerator.AllSpecialChars = "#?!@$%^&*-";
}
public PasswordGenerator(
int minimumLengthPassword = 8,
int maximumLengthPassword = 15,
int minimumLowerCaseChars = 2,
int minimumUpperCaseChars = 1,
int minimumNumericChars = 3,
int minimumSpecialChars = 1)
{
if (minimumLengthPassword < 1)
throw new ArgumentException("The minimumlength is smaller than 1.", nameof(minimumLengthPassword));
if (minimumLengthPassword > maximumLengthPassword)
throw new ArgumentException("The minimumLength is bigger than the maximum length.", nameof(minimumLengthPassword));
if (minimumLowerCaseChars < 0)
throw new ArgumentException("The minimumLowerCase is smaller than 0.", nameof(minimumLowerCaseChars));
if (minimumUpperCaseChars < 0)
throw new ArgumentException("The minimumUpperCase is smaller than 0.", nameof(minimumUpperCaseChars));
if (minimumNumericChars < 0)
throw new ArgumentException("The minimumNumeric is smaller than 0.", nameof(minimumNumericChars));
if (minimumSpecialChars < 0)
throw new ArgumentException("The minimumSpecial is smaller than 0.", nameof(minimumSpecialChars));
this._minimumNumberOfChars = minimumLowerCaseChars + minimumUpperCaseChars + minimumNumericChars + minimumSpecialChars;
if (minimumLengthPassword < this._minimumNumberOfChars)
throw new ArgumentException("The minimum length ot the password is smaller than the sum of the minimum characters of all catagories.", nameof(maximumLengthPassword));
this.MinimumLengthPassword = minimumLengthPassword;
this.MaximumLengthPassword = maximumLengthPassword;
this.MinimumLowerCaseChars = minimumLowerCaseChars;
this.MinimumUpperCaseChars = minimumUpperCaseChars;
this.MinimumNumericChars = minimumNumericChars;
this.MinimumSpecialChars = minimumSpecialChars;
this._allAvailableChars = this.OnlyIfOneCharIsRequired(minimumLowerCaseChars, PasswordGenerator.AllLowerCaseChars) + this.OnlyIfOneCharIsRequired(minimumUpperCaseChars, PasswordGenerator.AllUpperCaseChars) + this.OnlyIfOneCharIsRequired(minimumNumericChars, PasswordGenerator.AllNumericChars) + this.OnlyIfOneCharIsRequired(minimumSpecialChars, PasswordGenerator.AllSpecialChars);
}
private string OnlyIfOneCharIsRequired(int minimum, string allChars)
{
return minimum > 0 || this._minimumNumberOfChars == 0 ? allChars : string.Empty;
}
public string Generate()
{
int num = this._randomSecure.Next(this.MinimumLengthPassword, this.MaximumLengthPassword);
string str = this.GetRandomString(PasswordGenerator.AllLowerCaseChars, this.MinimumLowerCaseChars) + this.GetRandomString(PasswordGenerator.AllUpperCaseChars, this.MinimumUpperCaseChars) + this.GetRandomString(PasswordGenerator.AllNumericChars, this.MinimumNumericChars) + this.GetRandomString(PasswordGenerator.AllSpecialChars, this.MinimumSpecialChars);
string randomString = this.GetRandomString(this._allAvailableChars, num - str.Length);
return (str + randomString).ShuffleTextSecure();
}
private string GetRandomString(string possibleChars, int lenght)
{
string empty = string.Empty;
for (int index1 = 0; index1 < lenght; ++index1)
{
int index2 = this._randomSecure.Next(possibleChars.Length);
empty += possibleChars[index2].ToString();
}
return empty;
}
private static string GetCharRange(char minimum, char maximum, string exclusiveChars = "")
{
string first = string.Empty;
for (char ch = minimum; (int)ch <= (int)maximum; ++ch)
first += ch.ToString();
if (!string.IsNullOrEmpty(exclusiveChars))
first = new string(first.Except<char>((IEnumerable<char>)exclusiveChars).ToArray<char>());
return first;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
namespace Chrosey.Extensions.Helper
{
public class SemiNumericComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x.IsDate() && y.IsDate())
return DateTime.Parse(x).CompareTo(DateTime.Parse(y));
if (x.IsNumeric() && y.IsNumeric())
return x.CompareTo(y);
if (x.IsNumeric())
return 1;
return y.IsNumeric() ? -1 : string.Compare(x, y, true);
}
}
}

View File

@ -0,0 +1,9 @@
using System.DirectoryServices;
namespace Chrosey.Extensions.Interfaces
{
public interface ISearchResultWrapper
{
void Bind(SearchResult source);
}
}

27
RandomSecureVersion.cs Normal file
View File

@ -0,0 +1,27 @@
using System;
using System.Security.Cryptography;
namespace Chrosey.Extensions
{
internal class RandomSecureVersion
{
private readonly RNGCryptoServiceProvider _rngProvider = new RNGCryptoServiceProvider();
public int Next()
{
byte[] data = new byte[4];
this._rngProvider.GetBytes(data);
return BitConverter.ToInt32(data, 0);
}
public int Next(int maximumValue)
{
return this.Next(0, maximumValue);
}
public int Next(int minimumValue, int maximumValue)
{
return new Random(this.Next()).Next(minimumValue, maximumValue);
}
}
}

View File

@ -157,5 +157,55 @@ namespace Chrosey.Extensions
{
return string.Format(format, args);
}
public static string ReplaceUmlaute(this string input)
{
return input.ToLower().Replace("ä", "ae").Replace("ü", "ue").Replace("ö", "oe").Replace("ß", "ss").Replace("é", "e");
}
public static string PlaceUmlaute(this string input)
{
return input.Replace("ae", "ä").Replace("oe", "ö").Replace("ue", "ü").Replace("ss", "ß").Replace("aü", "aue").Replace("chäl", "chael").Replace("ßl", "ssl");
}
public static string ShuffleTextSecure(this string source)
{
return new string(source.ShuffleSecure<char>().ToArray<char>());
}
public static bool IsNumeric(this string value)
{
return int.TryParse(value, out int _);
}
public static bool IsDate(this string value)
{
return DateTime.TryParse(value, out DateTime _);
}
public static int LevenshteinDistance(this string a, string b)
{
int length1 = a.Length;
int length2 = b.Length;
if (length1 == 0)
return length2;
if (length2 == 0)
return length1;
int[,] numArray = new int[length1 + 1, length2 + 1];
int index1 = 0;
while (index1 <= length1)
numArray[index1, 0] = index1++;
int index2 = 0;
while (index2 <= length2)
numArray[0, index2] = index2++;
for (int index3 = 1; index3 <= length1; ++index3)
{
for (int index4 = 1; index4 <= length2; ++index4)
{
int num = (int)b[index4 - 1] == (int)a[index3 - 1] ? 0 : 1;
numArray[index3, index4] = Math.Min(Math.Min(numArray[index3 - 1, index4] + 1, numArray[index3, index4 - 1] + 1), numArray[index3 - 1, index4 - 1] + num);
}
}
return numArray[length1, length2];
}
}
}

View File

@ -0,0 +1,18 @@
using Chrosey.Extensions.Interfaces;
using System.DirectoryServices;
namespace Chrosey.Extensions.Wrapper
{
public abstract class ASearchResultWrapper : ISearchResultWrapper
{
public readonly string[] propertiesToLoad = new string[2]
{
"SAMAccountName",
"sn"
};
public virtual void Bind(SearchResult source)
{
}
}
}