How do I get the current username in .NET using C#?

Option A)

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  • Returns: NetworkName\Username
  • Gets the user’s Windows logon name.
  • Details: https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity

Option B)

string userName = Environment.UserName
  • Returns: Username
  • Gets the user name of the person who is associated with the current thread.
  • Details: https://docs.microsoft.com/en-us/dotnet/api/system.environment.username

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)