public static EnvironmentBlockHandle Create(TokenHandle token, IDictionary<string, string> extraEnvironmentVariables)
 {
     using (var environment = Create(token, false))
     {
         var current = environment.GetEnvironmentVariables();
         foreach (var extraEnvironmentVariable in extraEnvironmentVariables)
         {
             current[extraEnvironmentVariable.Key] = extraEnvironmentVariable.Value;
         }
         return Create(current);
     }
 }
Example #2
0
 public static void Main(string[] args)
 {
     using (var windowsIdentity = WindowsIdentity.GetCurrent())
     {
         using (var token = new TokenHandle(windowsIdentity))
         {
             var groups = token.GetGroupsTokenInformation(TokenInformationClass.TokenLogonSid);
             SecurityIdentifier securityIdentifier = groups.Single().SecurityIdentifier;
             Console.WriteLine("Is Account: {0}", securityIdentifier.IsAccountSid());
             Console.WriteLine("SID: {0}", securityIdentifier);
             Console.WriteLine("ProfilePath: {0}", token.GetUserProfileDirectory().FullName);
         }
     }
 }
Example #3
0
 public static ProcessInformation CreateAsUser(TokenHandle tokenHandle, string applicationName, string commandLine, bool inheritHandles, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environmentHandle, string currentDirectory, ProcessStartInfo startInfo, ProcessSecurity processSecurity = null, ThreadSecurity threadSecurity = null)
 {
     using (var processSecurityAttributes = processSecurity == null ? new SecurityAttributes() : new SecurityAttributes(processSecurity))
     {
         using (var threadSecurityAttributes = threadSecurity == null ? new SecurityAttributes() : new SecurityAttributes(threadSecurity))
         {
             ProcessInformationOut processInformation;
             if (!NativeMethods.CreateProcessAsUser(tokenHandle, applicationName, commandLine, processSecurityAttributes, threadSecurityAttributes, inheritHandles, creationFlags, environmentHandle ?? new EnvironmentBlockHandle(), currentDirectory, startInfo, out processInformation) || processInformation.ProcessHandle == IntPtr.Zero || processInformation.ThreadHandle == IntPtr.Zero)
             {
                 ErrorHelper.ThrowCustomWin32Exception();
             }
             return new ProcessInformation(processInformation.ProcessHandle, processInformation.ProcessId, processInformation.ThreadHandle, processInformation.ThreadId);
         }
     }
 }
Example #4
0
 public static extern bool DuplicateTokenEx(TokenHandle handle, TokenAccessRights desiredAccess, SecurityAttributes securityAttributes, SecurityImpersonationLevel impersonationLevel, TokenType tokenType, out TokenHandle newToken);
 public IEnvironmentBlock Create(TokenHandle token, bool inherit)
 {
     var handle = EnvironmentBlockHandle.Create(token, inherit);
     return new EnvironmentBlock(handle);
 }
Example #6
0
 public static extern bool CreateProcessWithTokenW(TokenHandle tokenHandle, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo, out ProcessInformationOut processInformation);
Example #7
0
 public static ProcessInformation CreateWithToken(TokenHandle tokenHandle, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo)
 {
     ProcessInformationOut processInformation;
     if (!NativeMethods.CreateProcessWithTokenW(tokenHandle, logonFlags, applicationName, commandLine, creationFlags, environment, currentDirectory, startupInfo, out processInformation) || processInformation.ProcessHandle == IntPtr.Zero || processInformation.ThreadHandle == IntPtr.Zero)
     {
         ErrorHelper.ThrowCustomWin32Exception();
     }
     return new ProcessInformation(processInformation.ProcessHandle, processInformation.ProcessId, processInformation.ThreadHandle, processInformation.ThreadId);
 }
Example #8
0
 public static extern bool LogonUser(string username, string domain, string password, UserLogonType logonType, UserLogonProvider logonProvider, out TokenHandle handle);
 public static extern bool LogonUser(string username, string domain, string password, UserLogonType logonType, UserLogonProvider logonProvider, out TokenHandle handle);
Example #10
0
 public Token(TokenHandle handle)
 {
     _handle = handle;
 }
Example #11
0
 public IProcessInformation CreateWithToken(TokenHandle token, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo)
 {
     return ProcessHandle.CreateWithToken(token, logonFlags, applicationName, commandLine, creationFlags, environment, currentDirectory, startupInfo);
 }
Example #12
0
 public IProcessInformation CreateAsUser(TokenHandle token, string applicationName, string commandLine, bool inheritHandles, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startInfo, ProcessSecurity processSecurity = null, ThreadSecurity threadSecurity = null)
 {
     return ProcessHandle.CreateAsUser(token, applicationName, commandLine, inheritHandles, creationFlags, environment, currentDirectory, startInfo, processSecurity, threadSecurity);
 }
Example #13
0
 public Token(TokenHandle handle)
 {
     _handle = handle;
 }
        public IToken Logon(string username, string domain, SecureString password, UserLogonType logonType = UserLogonType.Interactive, UserLogonProvider logonProvider = UserLogonProvider.Default)
        {
            var handle = TokenHandle.Logon(username, domain, password, logonType, logonProvider);

            return(new Token(handle));
        }
 public static EnvironmentBlockHandle Create(TokenHandle token, bool inherit)
 {
     EnvironmentBlockHandle environmentBlockHandle;
     if (!NativeMethods.CreateEnvironmentBlock(out environmentBlockHandle, token, inherit))
     {
         ErrorHelper.ThrowCustomWin32Exception();
     }
     return environmentBlockHandle;
 }
Example #16
0
 public static extern bool GetUserProfileDirectory(TokenHandle token, StringBuilder profileDir, ref int size);
Example #17
0
 public static extern bool LoadUserProfile(TokenHandle token, ref ProfileInfo profileInfo);
 public static extern bool DuplicateTokenEx(TokenHandle handle, TokenAccessRights desiredAccess, SecurityAttributes securityAttributes, SecurityImpersonationLevel impersonationLevel, TokenType tokenType, out TokenHandle newToken);
Example #19
0
 public static extern bool UnloadUserProfile(TokenHandle token, IntPtr profileInfo);
 public static extern bool LoadUserProfile(TokenHandle token, ref ProfileInfo profileInfo);
Example #21
0
 public static extern bool CreateProcessAsUser(TokenHandle tokenHandle, string applicationName, string commandLine, SecurityAttributes processAttributes, SecurityAttributes threadAttributes, bool inheritHandles, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startInfo, out ProcessInformationOut processInformation);
 public static extern bool UnloadUserProfile(TokenHandle token, IntPtr profileInfo);
Example #23
0
 public static extern bool OpenProcessToken(ProcessHandle processHandle, TokenAccessRights desiredAccess, out TokenHandle tokenHandle);
 public static extern bool GetUserProfileDirectory(TokenHandle token, StringBuilder profileDir, ref int size);
 public IEnvironmentBlock Create(TokenHandle token, IDictionary<string, string> extraEnvironmentVariables)
 {
     var handle = EnvironmentBlockHandle.Create(token, extraEnvironmentVariables);
     return new EnvironmentBlock(handle);
 }
 public static extern bool CreateEnvironmentBlock(out EnvironmentBlockHandle environment, TokenHandle token, bool inherit);