C# Console Application Throws error when debugging - when using breakpoint at the right location, no error is thrown

0

I'm currently editing a console Application on C# in Visual Studio 2019. It's an application to synchronize some data on a database. The last time I edited the code everything was fine and the code ran through.

At the moment I have a problem, where the app throws an catastrophic error in a passage, where I didn't change anything to the previous working code.

But if I set a breakpoint on the error throwing line in debug mode, it works just fine. I searched a lot on the internet but it doesn't seem, that anyone else had a similar problem.

That's the code snippet which throws the error without the breakpoint on it:

// GetADContext just creates a new PrincipalContext with specific user and password
PrincipalContext pc = SomeDllClass.GetADContext();
// The line below throws an error - but only if there is no breakpoint
UserPrincipal adUser = UserPrincipal.FindByIdentity(pc, IdentityType.Sid, myClass.SID);

I tried cleaning and rebuilding the solution. Also deleted the .vs, obj and bin Folder. Nothing worked so far.

I have no idea why this is happening. I hope anyone of you can help me with this weird behavior.

*Edit

Here is the full exception thrown:

"System.DirectoryServices.AccountManagement.PrincipalOperationException: Schwerwiegender Fehler (Ausnahme von HRESULT: 0x8000FFFF (E_UNEXPECTED)) ---> System.Runtime.InteropServices.COMException: Schwerwiegender Fehler (Ausnahme von HRESULT: 0x8000FFFF (E_UNEXPECTED))
   bei System.Security.Policy.PEFileEvidenceFactory.GetLocationEvidence(SafePEFileHandle peFile, SecurityZone& zone, StringHandleOnStack retUrl)
   bei System.Security.Policy.PEFileEvidenceFactory.GenerateLocationEvidence()
   bei System.Security.Policy.PEFileEvidenceFactory.GenerateEvidence(Type evidenceType)
   bei System.Security.Policy.AssemblyEvidenceFactory.GenerateEvidence(Type evidenceType)
   bei System.Security.Policy.Evidence.GenerateHostEvidence(Type type, Boolean hostCanGenerate)
   bei System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)
   bei System.Security.Policy.Evidence.GetHostEvidence(Type type, Boolean markDelayEvaluatedEvidenceUsed)
   bei System.Security.Policy.AppDomainEvidenceFactory.GenerateEvidence(Type evidenceType)
   bei System.Security.Policy.Evidence.GenerateHostEvidence(Type type, Boolean hostCanGenerate)
   bei System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)
   bei System.Security.Policy.Evidence.RawEvidenceEnumerator.MoveNext()
   bei System.Security.Policy.Evidence.EvidenceEnumerator.MoveNext()
   bei System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain, String exePath, String& typeName)
   bei System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath)
   bei System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   bei System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   bei System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord record)
   bei System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   bei System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   bei System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   bei System.Configuration.ConfigurationManager.GetSection(String sectionName)
   bei System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   bei System.DirectoryServices.SearchResultCollection.ResultsEnumerator..ctor(SearchResultCollection results, String parentUserName, String parentPassword, AuthenticationTypes parentAuthenticationType)
   bei System.DirectoryServices.SearchResultCollection.get_InnerList()
   bei System.DirectoryServices.SearchResultCollection.get_Count()
   bei System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
   --- Ende der internen Ausnahmestapelüberwachung ---
   bei System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
   bei System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
   bei System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
   bei System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
   bei System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
   bei IMSTools.Base.IMSStd.GetUserPrincipal(PrincipalContext context, IdentityType type, String value)
   bei IMSMitarbeiterSynch.Program.SynchADS(List`1 mitarbeiter) in D:\\Git\\IMSMitarbeiterSynch\\IMSMitarbeiterSynch\\IMSMitarbeiterSynch\\Program.cs:Zeile 240.
   bei IMSMitarbeiterSynch.Program.SynchronizeData() in D:\\Git\\IMSMitarbeiterSynch\\IMSMitarbeiterSynch\\IMSMitarbeiterSynch\\Program.cs:Zeile 88.
   bei IMSMitarbeiterSynch.Program.Main(String[] args) in D:\\Git\\IMSMitarbeiterSynch\\IMSMitarbeiterSynch\\IMSMitarbeiterSynch\\Program.cs:Zeile 53."
c#
active-directory
runtime-error
console-application
breakpoints
asked on Stack Overflow Mar 9, 2021 by Rico • edited Mar 9, 2021 by Jason Aller

1 Answer

0

So i figured it out myself.

At first i use an impersonation to get admin rights to a directory where i need some files:

if (Environment.UserName.ToLower().Trim() != "adminuser")
{
    if (loggedOn = LogonUser("adminuser", "domainName", "password", 2, 0, ref token))
    {
        WindowsIdentity newId = new WindowsIdentity(token);
        wic = newId.Impersonate();
    }
}

I did this at the start of the program. For the active-directory in the PrincipalContext i have to use another user for reading.

It looks like impersonation and PrincipalContext cannot operate correctly at the same time.

Solution:

The solution was to impersonate only when i need access to the directory and undo it afterwards, so that impersonation and `PrincipalContext` never were active at the same time.

Maybe someone has the same issue and can fix it with this solution
answered on Stack Overflow Mar 10, 2021 by Rico

User contributions licensed under CC BY-SA 3.0