mattjohnsonpint/SimpleImpersonation

System.Security.Principal.WindowsIdentity error message

Closed this issue · 6 comments

Hello all,

I am new to development and was hoping someone could point me in the right direction. My code works fine when I run it within VS, however, when I published it as a self-contained package and run the .exe file, I am getting the error message below:

Unhandled Exception: System.IO.FileNotFoundException
at ConsoleAppNetCore_CopyTable.Program.<>c.

b__0_0()
at System.Security.Principal.WindowsIdentity.<>c__DisplayClass64_0.b__0(Object )
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Security.Principal.WindowsIdentity.RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
at System.Security.Principal.WindowsIdentity.RunImpersonated(SafeAccessTokenHandle safeAccessTokenHandle, Action action)
at SimpleImpersonation.Impersonation.RunAsUser(UserCredentials credentials, LogonType logonType, Action action)
at ConsoleAppNetCore_CopyTable.Program.Main() in C:\Users\bphu\source\repos\ConsoleAppNetCore_CopyTable\ConsoleAppNetCore_CopyTable\Program.cs:line 206

Could you provide a more complete example of the code you're running? Also, are you running it on Windows, or some other OS? Is there anything else special about the way you're running the .exe?

Hi Matt,

I stripped my code to very bare bone, I created a self-contained .exe to my computer and it works fine, but when I published to a network location or another other place. I am getting the above error message. Please see my code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using SimpleImpersonation;
using System.Data.Common;
using System.Runtime;
using System.Reflection;

namespace MyTime
{

using System.Data.SqlClient;
class Program
{
    static void Main()
    {
        //try
        //{
            ///-------------  SimpleImpersonation ------------ ///
            var credentials = new UserCredentials("abccomp", "myUsername", "myPassword");
        Impersonation.RunAsUser(credentials, LogonType.NewCredentials, () =>
        {
            Console.WriteLine("Hello.  This is " + credentials);
            Console.ReadKey();
        }
        );
        
    }

}

}

LogonType.NewCredentials is only supported with LOGON32_PROVIDER_WINNT50
but in this library method LogonUser is always called with provider type param = 0

here is all available constants for provider type:

public const int LOGON32_PROVIDER_DEFAULT = 0;
public const int LOGON32_PROVIDER_WINNT35 = 1;
public const int LOGON32_PROVIDER_WINNT40 = 2;
public const int LOGON32_PROVIDER_WINNT50 = 3;

you need to check if logontype = LogonType.NewCredentials and then set provider type to 3

@sqladmin-zz - the default behavior should suffice, according to these docs (read the value/meaning under dwLogonProvider)

I was having trouble with an exception being thrown when trying to use LogonType.NewCredentials with a local user.

SimpleImpersonation.ImpersonationException: A logon request contained an invalid logon type value

The problem was that my username was not prefixed with .\. I solved it by changing:

new SimpleImpersonation.UserCredentials("Username", password);

To:

new SimpleImpersonation.UserCredentials(@".\Username", password);

It sound like this is resolved then. Thanks.