How to get UPN (User Principal Name) frof the current logged in user?

2.8k views Asked by At

In the context of my earlier question, I am trying to get UPN of the current logged in Windows Active directoy user in C++. I know how to get the SID of logged in user but unable to get the UPN of the logged in user.

1

There are 1 answers

0
Itaypk On

Assuming you are working as a server (with high privileges) against a client program, you can impersonate the user using the Windows impersonation API, then call GetUserNameExW with NameUserPrincipal as the name format. The tricky part here might be the impersonation, depending on what you have to work with.

If you only have the SID and impersonation is not an option, you can use WMI or LDAP and query the DC: search for the user entity with the matching SID and get the UserPrincipalName attribute. Again, you'll need sufficient permissions for this.