none
Unable to load DLL after Impersonating (0x80070542)

    Question

  • I have an application that prompts a user for credentials and impersonates the user before taking some action on that user's behalf.  However, when I try to call some framework methods that require .NET to load a native DLL, the call fails with the following error:

    System.DllNotFoundException: Unable to load DLL 'secur32.dll': Either a required impersonation level was not provided, or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)

    I've simplified the duplication scenario down to the following:

    this.userToken = IntPtr.Zero;
    bool authenticated = 
    	Win32.LogonUser(
    		username,
    		domain,
    		password,
    		LogonTypes.Network,
    		LogonProviders.Default,
    		out userToken);
    
    if (authenticated)
    {
    	this.impersonationContext = 
    		WindowsIdentity.Impersonate(this.userToken);
    	// This call blows up:
    	string userDomain = Environment.UserDomainName;
    }
    

    If I make a call to Environment.UserDomainName before impersonating, no exception is thrown (presumably the DLL is loaded before impersonation, so no attempt is made afterwards).

    Monday, October 11, 2010 4:44 PM

Answers

  • You might need to logon with the batch-type (LOGON32_LOGON_BATCH in Win32). The network type LOGON32_LOGON_NETWORK is documented as being used to authenticate passwords, not for impersonation.
    Phil Wilson
    • Marked as answer by eryang Tuesday, October 19, 2010 12:57 AM
    Monday, October 11, 2010 6:35 PM

All replies

  • You might need to logon with the batch-type (LOGON32_LOGON_BATCH in Win32). The network type LOGON32_LOGON_NETWORK is documented as being used to authenticate passwords, not for impersonation.
    Phil Wilson
    • Marked as answer by eryang Tuesday, October 19, 2010 12:57 AM
    Monday, October 11, 2010 6:35 PM
  • Hi Travis,

    I'm writing to check the issue status, please feel free to let us know if you have any concern.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, October 14, 2010 6:22 AM
  • I've got the same error on a win 7 enterprise machine.

    The Change from LOGON32_LOGON_INTERACTIVE to LOGON32_LOGON_BATCH doesn't fix it.

    I impersonate the same way as Travis and I also get the error after impersonate when calling

    Environment.UserDomainName;
    
    Monday, March 7, 2011 2:36 PM
  • Same error on windows server 2008 sp2.
    Wednesday, May 25, 2011 7:51 AM
  • Hi Travis, 

    your issue is already 4 years old and I would like to ask if you have found a solution for your problem.

    I am running into a similar problem.

    "Sometimes" even a simple "file exist" check fails, or currently I am trying to write to the eventlog and the .net framework fails to load the system.configuration assembly with the error 80070542.

    The interesting point is, that it occurs only rarely. Most time it works fine. If it fails I really have no idea what should be special about this time when it fails.

    Hopefully you have found a solution and perhaps it would work for me too :)

    Regards
    Alex


    Tuesday, January 27, 2015 4:00 PM