Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security.Principal;
- .
- .
- .
- WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
- String username = wp.Identity.Name;
- private string GetUserName(int SessionId)
- {
- try
- {
- Runspace runspace = RunspaceFactory.CreateRunspace();
- runspace.Open();
- Pipeline pipeline = runspace.CreatePipeline();
- pipeline.Commands.AddScript("Quser");
- pipeline.Commands.Add("Out-String");
- Collection<PSObject> results = pipeline.Invoke();
- runspace.Close();
- StringBuilder stringBuilder = new StringBuilder();
- foreach (PSObject obj in results)
- {
- stringBuilder.AppendLine(obj.ToString());
- }
- foreach (string User in stringBuilder.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Skip(1))
- {
- string[] UserAttributes = User.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries);
- if (int.Parse(UserAttributes[2].Trim()) == SessionId)
- {
- return UserAttributes[0].Replace(">", string.Empty).Trim();
- }
- }
- return stringBuilder.ToString();
- }
- catch (Exception ex)
- {
- }
- return string.Empty;
- }
- string CurrentUser = GetUserName(Process.GetCurrentProcess().SessionId);
- [DllImport("Wtsapi32.dll")]
- private static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WtsInfoClass wtsInfoClass, out IntPtr ppBuffer, out int pBytesReturned);
- [DllImport("Wtsapi32.dll")]
- private static extern void WTSFreeMemory(IntPtr pointer);
- private enum WtsInfoClass
- {
- WTSUserName = 5,
- WTSDomainName = 7,
- }
- private static string GetUsername(int sessionId, bool prependDomain = true)
- {
- IntPtr buffer;
- int strLen;
- string username = "SYSTEM";
- if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WtsInfoClass.WTSUserName, out buffer, out strLen) && strLen > 1)
- {
- username = Marshal.PtrToStringAnsi(buffer);
- WTSFreeMemory(buffer);
- if (prependDomain)
- {
- if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WtsInfoClass.WTSDomainName, out buffer, out strLen) && strLen > 1)
- {
- username = Marshal.PtrToStringAnsi(buffer) + "\" + username;
- WTSFreeMemory(buffer);
- }
- }
- }
- return username;
- }
- ManagementScope ms = new ManagementScope("\\.\root\cimv2");
- ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
- ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);
- foreach(ManagementObject mo in searcher.Get())
- {
- Console.WriteLine(mo["UserName"].ToString());
- }
- public string GetProcessOwner(string processName)
- {
- string query = "Select * from Win32_Process Where Name = "" + processName + """;
- ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
- ManagementObjectCollection processList = searcher.Get();
- foreach (ManagementObject obj in processList)
- {
- string[] argList = new string[] { string.Empty, string.Empty };
- int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
- if (returnVal == 0)
- {
- // return DOMAINuser
- string owner = argList[1] + "\" + argList[0];
- return owner;
- }
- }
- return "NO OWNER";
- }
- string username = "SYSTEM";
- var explorer = Process.GetProcessesByName("explorer").FirstOrDefault();
- if (explorer != null)
- {
- username = GetUsername(explorer.SessionId);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement