0

I have an app (C#, .NET4.5) placed on a remote computer, accessed and executed via local network. Let's call the computer where the app is placed "AppPC" and the computer that is exeuting the app "UserPC".

I want to achieve the same I would get with this, which is the first name (not username) of the current Windows User:

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

This code works just fine when the app is located on the same UserPC, but it throws an exception if the app is located on AppPC. More precisely is a System.DirectoryServices.Protocols.LdapException: "Can't establish connection. "

I can find the name of UserPC using this:

PrincipalContext ctx = new PrincipalContext(ContextType.Machine,null);
string pcName = ctx.ConnectedServer;

But from there, I do not know how to get the current user name.

I have tried getting all the UserPrincipal in ctx through a loop over a PrincipalSearcher.FindAll() but this only shows two users "Administrator" and "Guest" which is false, at least on UserPC. Maybe AppPC has that users, I'm not sure.

With System.Environment.UserName I can get the username but not the first name, which is what I'm interested in.

EDIT: Forgot to say that I have already checked this and doesn't work also (null): How do I get the current username in .NET using C#?

Community
  • 1
  • 1
Pinx0
  • 1,248
  • 17
  • 28
  • I don't think you should rely on being able to get the first name of the user. What your DisplayName property is returning will be whatever has been entered in active directory and therefore is subject to change by the network admins. – Rikalous Dec 19 '14 at 10:18
  • I know it can be changed by the admins, but in this environment I cant count on that being properly defined (Both Name, Surname and DisplayName) – Pinx0 Dec 19 '14 at 11:09

0 Answers0