Skip to content

Instantly share code, notes, and snippets.

@brunomartinspro
Created August 7, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunomartinspro/e8081c8fbcf8688ef593ed7d94229b2f to your computer and use it in GitHub Desktop.
Save brunomartinspro/e8081c8fbcf8688ef593ed7d94229b2f to your computer and use it in GitHub Desktop.
Reproduce System.AccessViolationException exception when using Active Directory Searcher
using System;
using System.DirectoryServices;
namespace BrunoMartinsPro.TriggerErrors
{
public class AdErrorUtils
{
public void TriggerError()
{
//Define connection properties
string ADConnectionString = "LDAP://AD.brunomartins.pro";
string ADUserLogin = "BrunoMartinsPro";
string ADUserPassword = "WoWWoWBadCommunication";
//Create entry
DirectoryEntry entry = new DirectoryEntry(ADConnectionString, ADUserLogin, ADUserPassword);
//Create searcher
DirectorySearcher searcher = new DirectorySearcher(entry);
//Define filter
searcher.Filter = "(SAMAccountname=" + "BrunoMartinsPro" + ")";
//Load ad properties
searcher.PropertiesToLoad.Add("name");
searcher.PropertiesToLoad.Add(null);
//Associate Directory Entry
searcher.SearchRoot = entry;
//Execute search
SearchResult adUser = searcher.FindOne();
//Execute search throws an exception:
//'System.AccessViolationException' in System.DirectoryServices.dll
//Attempted to read or write protected memory.This is often an indication that other memory is corrupt.
//This happens because no property NULL exists
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment