Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to determine if a user has logged in through a local account or through Active Directory. I know I can try and access the domain controller and trap an exception if it's local, however there must be some way to just query this.

Thank you,
Glenn
Posted
Comments
Nelek 10-May-14 17:42pm    
ZurdoDev 11-May-14 13:00pm    
Logged into what? Your app or windows?
David_Wimbley 11-May-14 18:43pm    
This is how you can tell...write the code and then it will tell...tah dah!

If it is your app i don't understand how you can not be able to tell if it is local or not.

Domain username example: Domain.com\MyUsername
Local username example: MyComputersMachineName\MyUsername

Pretty simple...If you come up with some edge case of "what if on a tuesday and the moon is the highest in the sky and the user finally logs in"...then check the username against the domain controller.
gmhanna 12-May-14 8:25am    
Hi David,

Where is that information kept that I can query in C#? I've tried the Environment.UserDomainName property however that throws an exception when trying to access it when the user is not logged on to a domain controller.

Thanks,
Glenn
David_Wimbley 12-May-14 13:41pm    
I ran it on my local and it didn't throw an exception.

Environment.UserDomainName

My local PC is Endor and it returns Endor.


You can also use Environment.Username i believe it is to then get the full username

Enviornment.UserDomainName + @"\" + Enviornment.Username == ENDOR\dwimbley

I have no clue what your code looks like as you didn't post anything so its possible that your exception is coming due to some other reason.

Also, not sure if this is a winforms/console/wpf/mvc/webforms app either so that could change things as well.

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900