public void GeneratesEnvironmentForUserToken() { var identity = WindowsIdentity.GetCurrent(); var userToken = identity.Token; var env = EnvironmentBlock.CreateForUser(userToken); var dict = env.ToDictionary(); Assert.Equal(GetUserName(identity), dict["USERNAME"]); }
/// <summary> /// Returns default environment for the process. /// If credentials are specified then the default environment is the default for that user. /// Otherwise the default is to inherit from this process. /// </summary> private Dictionary <string, string> CreateDefaultProcessEnvironment(NetworkCredential credential) { EnvironmentBlock envBlock = new EnvironmentBlock(); if (credential == null) { envBlock = EnvironmentBlock.Create(Environment.GetEnvironmentVariables()); } else { using (var safeUserToken = Utils.LogonAndGetUserPrimaryToken(credential)) { envBlock = EnvironmentBlock.CreateForUser(safeUserToken.DangerousGetHandle()); } } return(envBlock.ToDictionary()); }