Skip to content

Instantly share code, notes, and snippets.

@neslekkim
Last active December 23, 2015 04:19
Show Gist options
  • Save neslekkim/6579435 to your computer and use it in GitHub Desktop.
Save neslekkim/6579435 to your computer and use it in GitHub Desktop.
Trouble reading AD.
This code works for all users belonging to domain admins, but not normal users, and this is for only one of our customers, so far.
WindowsIdentity user = WindowsIdentity.GetCurrent();
string username = user.Name;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, username); <-- gives exception:
System.Runtime.InteropServices.COMException (0x8007200A): Det angitte attributtet eller verdien for katalogtjenesten finnes ikke.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_SchemaEntry()
at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de)
at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options)
at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment