private WindowsIdentity CreateWindowsIdentity(string username, string domain, string password, SecurityLogOnType logonType, LogOnProviderType logonProviderType, SecurityImpersonationLevel impersonationLevel)
        {
            // initialize tokens
            var existingTokenHandle = IntPtr.Zero;
            var duplicateTokenHandle = IntPtr.Zero;

            if (!NativeMethods.LogonUser(
                username,
                domain,
                password,
                (int)logonType,
                (int)logonProviderType,
                out existingTokenHandle))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            if (!NativeMethods.DuplicateToken(existingTokenHandle, (int)impersonationLevel, out duplicateTokenHandle))
            {
                NativeMethods.CloseHandle(existingTokenHandle);
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            // create new identity using new primary token
            return new WindowsIdentity(duplicateTokenHandle);
        }
        private WindowsIdentity CreateWindowsIdentity(string username, string domain, string password, SecurityLogOnType logonType, LogOnProviderType logonProviderType, SecurityImpersonationLevel impersonationLevel)
        {
            // initialize tokens
            var existingTokenHandle  = IntPtr.Zero;
            var duplicateTokenHandle = IntPtr.Zero;

            if (!NativeMethods.LogonUser(
                    username,
                    domain,
                    password,
                    (int)logonType,
                    (int)logonProviderType,
                    out existingTokenHandle))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            if (!NativeMethods.DuplicateToken(existingTokenHandle, (int)impersonationLevel, out duplicateTokenHandle))
            {
                NativeMethods.CloseHandle(existingTokenHandle);
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            // create new identity using new primary token
            return(new WindowsIdentity(duplicateTokenHandle));
        }
            public NewIdentityHandle(string userName, string domain, string password, SecurityLogOnType logOnType, LogOnProviderType logOnProvider, SecurityImpersonationLevel impersonationLevel)
            {
                if (!NativeMethods.LogonUser(
                        userName,
                        domain,
                        password,
                        (int)logOnType,
                        (int)logOnProvider,
                        out var logonHandle))
                {
                    throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

#if NETSTANDARD
                Handle = logonHandle;
#else
                // adapted from:
                // https://www.codeproject.com/csharp/cpimpersonation1.asp
                if (!NativeMethods.DuplicateToken(logonHandle, (int)impersonationLevel, out _handle))
                {
                    NativeMethods.CloseHandle(logonHandle);
                    throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                NativeMethods.CloseHandle(logonHandle);

                // create new identity using new primary token)
                Handle = new WindowsIdentity(_handle);
#endif
            }