user_dll_visualstudio/User/User.cs
Hoeglinger Eugen 95fe18b0a3 Initialized
- Liest den aktuell angemeldeten User aus
- Prüft ob dieser auch einen '.adm' Account hat
- Prüft ob dieser auch einen '.cadm' Account hat
2022-06-01 14:56:48 +02:00

222 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Eugen.ESystem
{
public class User
{
#region Version und Copyright
// Version und Copyright
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
static string date = value.ToShortDateString();
static string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
if (startYear.Contains("-"))
{
startYear = startYear.Remove(startYear.IndexOf("-"));
}
else
{
startYear = startYear.Remove(startYear.IndexOf(" "));
}
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
string start = temp.Remove(temp.IndexOf(startYear) - 1);
string end = (temp.Remove(0, temp.IndexOf(" by")));
return String.Concat(start, " ", startYear, "-", currentYear, end);
}
}
#endregion
#region DLL-Info
/// <summary>
/// Contains the name of the dll file.
/// </summary>
public static string DllName { private set; get; }
/// <summary>
/// Contains the version of the dll file.
/// </summary>
public static string DllVersion { private set; get; }
/// <summary>
/// Contains the copyright notice.
/// </summary>
public static string Copyright { private set; get; }
/// <summary>
/// Contains the name-, version- and copyright-information of the dll file.
/// </summary>
public static string[] DllInfo { private set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
string[] dllInfo = new string[3];
dllInfo[0] = dllName;
dllInfo[1] = dllVersion;
dllInfo[2] = copyright;
DllInfo = dllInfo;
}
protected string ProgramName { private set; get; }
protected string ProgramVersion { private set; get; }
protected string ProgramCopyright { private set; get; }
#endregion
static User()
{
SetDllInfo();
}
/// <summary>
/// Current User
/// </summary>
/// <returns>Returns the current user</returns>
public static string Current()
{
return Environment.UserName;
}
/// <summary>
/// Domain of the current user
/// </summary>
/// <returns>Returns the domain of the current user</returns>
public static string Domain()
{
return CurrentDomainUser().Remove(CurrentDomainUser().IndexOf("\\"));
}
/// <summary>
/// Is the user a 'cadm'
/// </summary>
/// <returns>If the user is a 'cadm' true is returned, otherwise false</returns>
public static bool IsCadm()
{
if (UserExists(String.Concat(Current(), ".cadm"), User.Domain(), null, null, null))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Is the user a 'adm'
/// </summary>
/// <returns>If the user is a 'adm' true is returned, otherwise false</returns>
public static bool IsAdm()
{
if (UserExists(String.Concat(Current(), ".adm"), User.Domain(), null, null, null))
{
return true;
}
else
{
return false;
}
}
private static bool UserExists(string userName, string domainName, string machineName, string authenticationUser, string authenticationPassword)
{
if (domainName == null && machineName == null)
{
machineName = Environment.MachineName;
}
string adsiPath = "WinNT://";
if (domainName != null && machineName != null)
{
adsiPath += domainName + "/" + machineName + ",computer";
}
else if (machineName != null)
{
adsiPath += machineName + ",computer";
}
else if (domainName != null)
{
adsiPath += domainName + ",domain";
}
DirectoryEntry computerEntry = new DirectoryEntry(adsiPath, authenticationUser, authenticationPassword);
try
{
DirectoryEntry userEntry = computerEntry.Children.Find(userName, "user");
return true;
}
catch
{
return false;
}
}
private static string CurrentDomainUser()
{
return System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
}
}