|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
About Environment.UserDomainNameHello,
I'm writing a .NET v1.1 Windows Application which will try to determine current username by Environment.UserDomainName + "\\" + Environment.UserName. This approach works in normal condition if I run it directly. (returns "<DomainName>\<Username>") But if I use the program with RunAs.exe, let's say run with "<DomainName>\Administrator" account, it'll return "<MachineName>\administrator" instead. Can anyone suggest what I should use to determine the current username? Regards, Lau Lei Cheong This is probably because you are running the program as the local computer
admin, and you are not connected to a domain at the time. There is no way to detect the domain when you're not connected to one. Show quote "Lau Lei Cheong" <leu***@yehoo.com.hk> wrote in message news:Ovtah9z6FHA.2036@TK2MSFTNGP14.phx.gbl... > Hello, > > I'm writing a .NET v1.1 Windows Application which will try to determine > current username by Environment.UserDomainName + "\\" + > Environment.UserName. This approach works in normal condition if I run it > directly. (returns "<DomainName>\<Username>") > > But if I use the program with RunAs.exe, let's say run with > "<DomainName>\Administrator" account, it'll return > "<MachineName>\administrator" instead. > > Can anyone suggest what I should use to determine the current username? > > Regards, > Lau Lei Cheong > > No. I'm currently connected to the domain, and I'm sure the connection to
Domain Controller is Ok (This is obvious, I can't use runas to run something as Domain Administrator if I can't anthenicate against the domain server. As I won't normally logon my desktop with that identity, my desktop can't possibly have cached the password). Anyway, I've find the System.Security.Principal.WindowsIdentity.GetCurrent().Name offers exactly what I wanted, so while the UserDomainName problem persist, I can leave it as a report to the Framework development team. Show quote "Robert Jeppesen durius (dot) com>" <robert<at> ¼¶¼g©ó¶l¥ó·s»D:OTe10H06FHA.3***@TK2MSFTNGP14.phx.gbl... > This is probably because you are running the program as the local computer > admin, and you are not connected to a domain at the time. There is no way > to detect the domain when you're not connected to one. > > -- > Robert Jeppesen > Durius > http://www.durius.com/ > > "Lau Lei Cheong" <leu***@yehoo.com.hk> wrote in message > news:Ovtah9z6FHA.2036@TK2MSFTNGP14.phx.gbl... >> Hello, >> >> I'm writing a .NET v1.1 Windows Application which will try to >> determine current username by Environment.UserDomainName + "\\" + >> Environment.UserName. This approach works in normal condition if I run it >> directly. (returns "<DomainName>\<Username>") >> >> But if I use the program with RunAs.exe, let's say run with >> "<DomainName>\Administrator" account, it'll return >> "<MachineName>\administrator" instead. >> >> Can anyone suggest what I should use to determine the current >> username? >> >> Regards, >> Lau Lei Cheong >> >> > > Did you runas using /env ?
Willy. Show quote "Lau Lei Cheong" <leu***@yehoo.com.hk> wrote in message news:uzzU7$06FHA.3136@TK2MSFTNGP09.phx.gbl... > No. I'm currently connected to the domain, and I'm sure the connection to > Domain Controller is Ok (This is obvious, I can't use runas to run > something as Domain Administrator if I can't anthenicate against the > domain server. As I won't normally logon my desktop with that identity, my > desktop can't possibly have cached the password). > > Anyway, I've find the > System.Security.Principal.WindowsIdentity.GetCurrent().Name offers exactly > what I wanted, so while the UserDomainName problem persist, I can leave it > as a report to the Framework development team. > > "Robert Jeppesen durius (dot) com>" <robert<at> > ¼¶¼g©ó¶l¥ó·s»D:OTe10H06FHA.3***@TK2MSFTNGP14.phx.gbl... >> This is probably because you are running the program as the local >> computer admin, and you are not connected to a domain at the time. There >> is no way to detect the domain when you're not connected to one. >> >> -- >> Robert Jeppesen >> Durius >> http://www.durius.com/ >> >> "Lau Lei Cheong" <leu***@yehoo.com.hk> wrote in message >> news:Ovtah9z6FHA.2036@TK2MSFTNGP14.phx.gbl... >>> Hello, >>> >>> I'm writing a .NET v1.1 Windows Application which will try to >>> determine current username by Environment.UserDomainName + "\\" + >>> Environment.UserName. This approach works in normal condition if I run >>> it directly. (returns "<DomainName>\<Username>") >>> >>> But if I use the program with RunAs.exe, let's say run with >>> "<DomainName>\Administrator" account, it'll return >>> "<MachineName>\administrator" instead. >>> >>> Can anyone suggest what I should use to determine the current >>> username? >>> >>> Regards, >>> Lau Lei Cheong >>> >>> >> >> > > Hello Lau,
Environment.UserDomainName returns the computer name (and not the domain name as you would expect), if there is a local account with the same name as the domain account with which you are logged in. For example, when logged into MYDOMAIN\user1, but there is also a local user account with the name user1, then .UserDomainName does not return MYDOMAIN as you would expect, but rather the computer name. You can get the "true" domain user domain name, using Environment.ExpandEnvironmentVariables("%USERDOMAIN%"). I attach a solution that demonstrates this. Regards Joubert Show quote "Lau Lei Cheong" <leu***@yehoo.com.hk> wrote in message [attached file: ShowCredentials.zip]news:Ovtah9z6FHA.2036@TK2MSFTNGP14.phx.gbl... > Hello, > > I'm writing a .NET v1.1 Windows Application which will try to determine > current username by Environment.UserDomainName + "\\" + > Environment.UserName. This approach works in normal condition if I run it > directly. (returns "<DomainName>\<Username>") > > But if I use the program with RunAs.exe, let's say run with > "<DomainName>\Administrator" account, it'll return > "<MachineName>\administrator" instead. > > Can anyone suggest what I should use to determine the current username? > > Regards, > Lau Lei Cheong > >
Other interesting topics
|
|||||||||||||||||||||||