HELLO I TRIED YOUR SOLUTION BUT I HAVE THE ERROR.
Select your site's application pool
Select Advanced Settings on the right-hand side
On the Advanced Settings pop-up window, scroll down to the Process Model group
Change the first option called Identity to NetworkService (mine was set to the default ApplicationPoolIdentity).
MY CODE IS WORKING IN MY SEREVER DEV BUT IN SERVER PROD NOT WORK :
public bool DoesUserExist(string userName)
using (var domainContext = new PrincipalContext(ContextType.Domain, "XXXXXXXXXXXXX"))
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
return foundUser != null;