Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 956397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:31:58+00:00 2026-05-16T00:31:58+00:00

System.Environment.UserName internally calls [DllImport(advapi32.dll, CharSet=CharSet.Auto)] internal static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:31:58+00:00Added an answer on May 16, 2026 at 12:31 am

    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?

    if (!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated )
      System.Threading.Thread.CurrentPrincipal = 
        new System.Security.Principal.WindowsPrincipal(
           System.Security.Principal.WindowsIdentity.GetCurrent());
    
    userName = System.Threading.Thread.CurrentPrincipal.Identity.Name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between System.Web.HttpContext.Current.User.Identity.Name and System.Environment.UserName in the context of a ASP.Net
Process myProcess = new Process(); ProcessStartInfo remoteAdmin = new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + @\iisreset.exe /restart); remoteAdmin.UserName
System.Environment.OSVersion does not appear to indicate which Edition of Windows 2003 is installed (Standard,
Is is possible to read system environment variables in a Windows Scripting Host (WSH)
I need to replace all System.Environment.Newline(s) in the string returned by my function with
How can I read the value of a system environment variable in a T-SQL
Can I create a junit environment without file system and network access? I want
Here is the error: System.TypeInitializationException: The type initializer for 'NHibernate.Cfg.Environment' threw an exception. --->
In Java, reading environment variables is done with System.getenv() . Is there a way
I have some inherited code which stores SMTP server, username, password in the system.net/mailSettings/smtp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.