'.net core Getting null User Name from ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem")
I am stucked trying to get username from using the following post's code
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string) collection.Cast<ManagementBaseObject>().First()["UserName"];
This works when I debug locally on my PC Windows 10 and Visual Studio. Also Environment.Username brings back my login UserName.
However, when I deployed to Windows Server, I get the service account for Environment.Username and null for the WMI call.
I also tried another post's approach
foreach (System.Management.ManagementObject Process in Processes.Get())
{
if (Process["ExecutablePath"] != null &&
System.IO.Path.GetFileName(Process["ExecutablePath"].ToString()).ToLower() == "explorer.exe" )
{
string[] OwnerInfo = new string[2];
Process.InvokeMethod("GetOwner", (object[])OwnerInfo);
Console.WriteLine(string.Format("Windows Logged-in Interactive UserName={0}", OwnerInfo[0]));
break;
}
}
On my .net core, I have to use allowAnonymous and WindowsAuthentication. I am reading a few posts that said there is a way to get the logged in user name and not the service account.
So far, no success.
Any help is appreciated. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
