System.Environment.UserName internally calls
[DllImport(“advapi32.dll”, CharSet=CharSet.Auto)]
internal static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);
Each call seems to hit AD, thus network latency and AD query effect speed of execution.
Would you know if there is a better way to retrieve this value?
Something like a cached SID somewhere on the Thread maybe?
So I could read UserName & SID and cache these locally (for the time of execution) and only query System.Environment.UserName when I will get new SID (or something along these lines).
Thanks,
Code bellow seems to do the trick.
First get current principal on thread if not set already.
each next time just use it, adds small overhead to the thread itself, but doesn’t call AD each time.
Any thoughts?