简体   繁体   中英

How does PrincipalContext login to domain server

I have the following C# code that connects to my domain server and performs some actions on it. Everything works fine on my computer and I can run all my commands just fine.

My questions is: what credentials are used for the connection to the server? I assume it uses the current users credentials. So my real question is will this work on a normal user. I am an admin and it works fine on my machine.

However I am wondering if the same would hold true for a non admin?

PrincipalContext AD = new PrincipalContext(ContextType.Domain, "172.18.4.4");

UserPrincipal u = new UserPrincipal(AD);
u.SamAccountName = Environment.UserName;

PrincipalSearcher search = new PrincipalSearcher(u);

UserPrincipal result = (UserPrincipal)search.FindOne();

If that code is running in a Windows app then the credentials used are the ones of the current Windows user and it should work fine in a domain. If the code is running in an ASP.NET site, then the credentials are the ones of the application pool in which the site is running. In this last case, you might need to play with the Identity of the app pool: LocalSystem, NetworkService...

In any of the previous cases, you can impersonate a user to run this code under: (look at the answer)

How to use LogonUser properly to impersonate domain user from workgroup client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM