Home All Groups Group Topic Archive Search About

About Environment.UserDomainName

Author
17 Nov 2005 6:22 AM
Lau Lei Cheong
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

Author
17 Nov 2005 6:49 AM
Robert Jeppesen
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/

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
>
>
Author
17 Nov 2005 8:21 AM
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
>>
>>
>
>
Author
20 Nov 2005 9:31 PM
Willy Denoyette [MVP]
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
>>>
>>>
>>
>>
>
>
Author
21 Nov 2005 10:07 PM
Mattias Sjögren
Willy, you seem to be one day behind the rest of us. :)


Mattias
Author
11 Jan 2006 10:35 PM
Joubert Nel
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
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
>
>

[attached file: ShowCredentials.zip]

AddThis Social Bookmark Button