Question

Whenever I make a call to this method, I crash.

    public bool findSAMAccountByName(string environmentUserName)
    {
        try
        {
            const string Domain = "someserver";
            // set up domain context
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Domain);

            // find a user
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, environmentUserName);

            if (user != null)
            {
                // do something here....     
                string samAccountName = user.SamAccountName;
                return true;
            }
            return false;
        }

        catch (Exception e)
        {
            e.ToString();
        }
        return false;
    }

The innerexception says

{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

StackTrace is

 at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetEx(String bstrName, Object& value)
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   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)
   at ActiveRecord.MainWindow.findSAMAccountByName(String environmentUserName) in MainWindow.xaml.cs:line 60
   at ActiveRecord.MainWindow..ctor() in MainWindow.xaml.cs:line 39

My call to this method is causing the crash

                if (findSAMAccountByName(Environment.UserName))
                {
                    Console.WriteLine("Username found");
                }

If I remove/comment the code, it compiles and runs fine.


UPDATE: After following DJ KRAZE's advice

const string Domain = "someserver";

After changing the PrincipalContext to accept 2 agruements, the new StackTrace is

      at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
Was it helpful?

Solution

Cocoa Dev something like this should work..

const string Domain = "SomeServer.Foo.Bar.com";
const string Username = "someUserSamAccountName";

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  Username);

replace with your values and this should return if a user is found or not.. I work with AD on a daily basis and just tested with my account creds hope this helps if so change your Method to pass in string samAccount , string DomanName;

hard coded example that you can follow

const string Domain = "SLO1.Foo.Bar.biz:389";
const string Container = @"DC=Foo,DC=Bar,DC=biz";
const string Username = @"cocoadev";//replace with actual samAccountName
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, Container);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  username);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top