Information about the current logged user !

D

Depechie

Hey guys,

small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...

What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :

[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);

But is there no .Net code available ??

Greets
Glenn
 
N

Nicholas Paldino [.NET/C# MVP]

That will get the OP only part of the way there. Once you have the
WindowsIdentity, you can use the name of the user or the SID (which
WindowsIdentity will provide as well) and pass it to the LookupAccountName
or LookupAccountSid API functions to get the domain that the user is a part
of. Once that is obtained, you can compare it against
Environment.MachineName.

You can also use WMI, using the Win32_UserInDomain class to see if the
user is in or not in a particular domain.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Muth said:
See System.Security.Principal.WindowsIdentity.GetCurrent()

Brian

Depechie said:
Hey guys,

small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...

What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :

[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);

But is there no .Net code available ??

Greets
Glenn
 
W

Willy Denoyette [MVP]

Depechie said:
Hey guys,

small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...

What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :

[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);

But is there no .Net code available ??

Greets
Glenn


Get the MACHINE (System.Environment.MachineName) name and DOMAINNAME
(System.Environment.UserDomainName) from the environment, if both are equal,
you are running in a local logon session, else you are running in a domain
session with DOMAINNAME as the logon domain.

Willy.
 
D

Depechie

Hey guys,
small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...
What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :
[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);
But is there no .Net code available ??
Greets
Glenn

Get the MACHINE (System.Environment.MachineName) name and DOMAINNAME
(System.Environment.UserDomainName) from the environment, if both are equal,
you are running in a local logon session, else you are running in a domain
session with DOMAINNAME as the logon domain.

Willy.

Hey Brian and Willy,

So there is no function that actually tells me if it is a domain user
or not ?
Because I also saw the difference in DomainName and MachineName, but
that is also String checking.

But ok, if that is the only way to go, then I was on the correct
track !
Weird that .Net doesn't provide this information !! Because Windows
needs it when you start up your pc, so somehow it has to know where
the user is !
Strange....

But thanks for the nice answers
 
B

Brian Muth

So there is no function that actually tells me if it is a domain user
or not ?
Because I also saw the difference in DomainName and MachineName, but
that is also String checking.

Gosh, I thought Willy gave a very elegant solution.

But ok, if that is the only way to go, then I was on the correct
track !
Weird that .Net doesn't provide this information !! Because Windows
needs it when you start up your pc, so somehow it has to know where
the user is !

I suspect Windows does something very similar to what Willy describes.

Brian
 
W

Willy Denoyette [MVP]

Depechie said:
Hey guys,
small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...
What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :
[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);
But is there no .Net code available ??
Greets
Glenn

Get the MACHINE (System.Environment.MachineName) name and DOMAINNAME
(System.Environment.UserDomainName) from the environment, if both are
equal,
you are running in a local logon session, else you are running in a
domain
session with DOMAINNAME as the logon domain.

Willy.

Hey Brian and Willy,

So there is no function that actually tells me if it is a domain user
or not ?
Because I also saw the difference in DomainName and MachineName, but
that is also String checking.

But ok, if that is the only way to go, then I was on the correct
track !
Weird that .Net doesn't provide this information !! Because Windows
needs it when you start up your pc, so somehow it has to know where
the user is !
Strange....

But thanks for the nice answers



You can use System.Management and WMI to see whether the current user is a
domain user, but it's actually more complex than a simple string comparison
and it's slow when the DC is on a slow connection or (worse) not reachable.


using System.Management;
....

string account = WindowsIdentity.GetCurrent().Name.Replace(@"\", @"\\");
string queryString = "select LocalAccount from win32_useraccount where
caption='" + account +"'";

using(ManagementObjectSearcher query = new ManagementObjectSearcher(new
SelectQuery(queryString)))
{
foreach( ManagementObject mo in query.Get()) {
Console.WriteLine( "Localuser '{0}' ", mo["LocalAccount"].ToString());
}
}

Willy.
 
D

Doug Semler

Hey guys,
small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...
What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :
[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);
But is there no .Net code available ??
Greets
Glenn
Get the MACHINE (System.Environment.MachineName) name and DOMAINNAME
(System.Environment.UserDomainName) from the environment, if both are equal,
you are running in a local logon session, else you are running in a domain
session with DOMAINNAME as the logon domain.

Hey Brian and Willy,

So there is no function that actually tells me if it is a domain user
or not ?
Because I also saw the difference in DomainName and MachineName, but
that is also String checking.

But ok, if that is the only way to go, then I was on the correct
track !
Weird that .Net doesn't provide this information !! Because Windows
needs it when you start up your pc, so somehow it has to know where
the user is !
Strange....

But thanks for the nice answers

I guess you COULD theoretically do this...but it assumes that the
Domain Computers account hasn't changed <g>

static bool IsCurrentUserInDomain()
{
// Assumes Domain Computers name hasn't been changed (can it?)
NTAccount acct = new NTAccount("Domain Computers");
SecurityIdentifier domusers = null;
try
{
// Translate it into an SID
domusers =
(SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
}
catch (IdentityNotMappedException)
{
// Computer not in Domain
return false;
}
return WindowsIdentity.GetCurrent().User.IsEqualDomainSid(domusers);
}
 
D

Doug Semler

Hey guys,
small question. Can I find out if the current logged on use is a local
user or a domain user ?
And please don't use any solution that uses String parsing...
What I need is to get the display name of the current user and the
only way I'm able to do this is using a DLL wrapper :
[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat,
StringBuilder userName, ref int userNameSize);
But is there no .Net code available ??
Greets
Glenn
Get the MACHINE (System.Environment.MachineName) name and DOMAINNAME
(System.Environment.UserDomainName) from the environment, if both are equal,
you are running in a local logon session, else you are running in a domain
session with DOMAINNAME as the logon domain.
Willy.
Hey Brian and Willy,
So there is no function that actually tells me if it is a domain user
or not ?
Because I also saw the difference in DomainName and MachineName, but
that is also String checking.
But ok, if that is the only way to go, then I was on the correct
track !
Weird that .Net doesn't provide this information !! Because Windows
needs it when you start up your pc, so somehow it has to know where
the user is !
Strange....
But thanks for the nice answers

I guess you COULD theoretically do this...but it assumes that the
Domain Computers account hasn't changed <g>

static bool IsCurrentUserInDomain()
{
// Assumes Domain Computers name hasn't been changed (can it?)
NTAccount acct = new NTAccount("Domain Computers");
SecurityIdentifier domusers = null;
try
{
// Translate it into an SID
domusers =
(SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
}
catch (IdentityNotMappedException)
{
// Computer not in Domain
return false;
}
return WindowsIdentity.GetCurrent().User.IsEqualDomainSid(domusers);



}

Huh. That won't work if the user logged in is on machine in domain 1,
but account is from domain 2 in the forrest, would it? Maybe use
Domain Users? <shrug> Best way is to get current login and compare
the "domain" portion to the machine name. <G>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top