One of my colleagues asked this question recently and I was wondering if anybody here had any ideas
"We are using Environment.UserDomainName in the new Corporate System for some of the security checking. An interesting point arose today. If you are on a machine logged onto a domain, Environment.UserDomainName will return the name of the domain, UNLESS there is an account on the local machine with the same name, in which case it will return the name of the local machine. This means you can not be guaranteed that it will return the results you are expecting.
The obvious workaround is to use Environment.GetEnvironmentVariable("USERDOMAIN"), but the problem with that is it does not take any account of things like 'Run As'. Anybody got any ideas of a bullet-proof way to determine the domain the current account is a member of? I thought of using System.Security.Principal.WindowsIdentity.GetCurrent().Name, but that doesn't work for Run As on a machine that is not part of the domain. "