views:

326

answers:

2

I am new to Active Directory.

I have a VBA Excel Add-In that should run if, and only if, the computer that it is running on is currently logged into the Active Directory, whether locally or through a VPN.

Knowing the domain name, how would I retrieve the user name for the currently logged in user?

Thanks!

A: 

Try this

MsgBox Environ("USERNAME")
Rubens Farias
That works if the user is logged in locally, but in some situations the user can log into the VPN using a different user name than their Windows user name. In those situations, I need the VPN user name. Thanks though!
Kuyenda
if your're executing your VBA code inside VPN, you'll get VPN username, not your local user.
Rubens Farias
Ruben, thanks for your suggestions. If I understand you correctly, you mean that I should store and run the add-in remotely. Unfortunately, I have to run it locally.
Kuyenda
Sorry, maybe I'm missing something; can you please elaborate on your VPN scenario?
Rubens Farias
Sure. I simply have a VPN client installed on my system. I connect the VPN using the username assigned to me, but that is not the same user name as my Windows username. Once the VPN is connected, all applications are able to connect to the relevant servers, otherwise connection is not possible. The VPN is not overriding the Windows authentication, although I know it is possible to do that. I need to retrieve the VPN username when the VPN is connected.
Kuyenda
Looks like the VPN is connected to the network but not the Active Directory. Someone else had a similar situation (http://stackoverflow.com/questions/1043436/how-do-i-get-the-current-user-identity-for-a-vpn-user-in-a-windows-forms-app). So, I guess I have to try to query the username from the VPN itself.
Kuyenda
Note that using any Environ variable is *NOT* secure. While unlikely a person can start the Command Prompt, change the environment variable and then run Excel from the command prompt. Excel will now happily use that value.
Tony Toews
+2  A: 

EDITED: If I understand your situation properly, then you might be going about this the wrong way.

When your app starts up, you could do a simple ping against a machine that the user would only be able to see if they were connected to your network, whether they log into the local network or if they are connected via the VPN.

If they already have access to your local network, it means they've already authenticated against whatever machanism, whether it's Active Directory or something else, and it means they are "currently logged in".

On a side note, Active Directory by itself doesn't know if someone is logged in. There's no way you can do something like:

ActiveDirectory.getIsThisUserLoggedIn("username");

Active Directory only acts as a mechanism for user metadata, security, and authentication.

Alex Beardsley
This is what I needed to know: "Active Directory by itself doesn't know if someone is logged in." Thanks Nalandial!
Kuyenda