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"]);
        }
        public void CanBeBuiltFromDictionary()
        {
            var env = EnvironmentBlock.Create(new Dictionary <string, string> {
                { "FOO", "BAR" }
            });
            var dict = env.ToDictionary();

            Assert.Equal(1, dict.Count);
            Assert.Equal("BAR", dict["FOO"]);
        }
        public void MergeOverwritesOld()
        {
            var env    = EnvironmentBlock.CreateSystemDefault();
            var newEnv = EnvironmentBlock.Create(new Hashtable {
                { "TMP", "FOOBAR" }
            });
            var dict = env.Merge(newEnv).ToDictionary();

            Assert.Equal(env.ToDictionary().Count, dict.Count);
            Assert.Equal("FOOBAR", dict["TMP"]);
        }
Example #4
0
        public static EnvironmentBlock Create(IDictionary dictionary)
        {
            Dictionary <string, string> typedDictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var key in dictionary.Keys)
            {
                typedDictionary[key.ToString()] = dictionary[key].ToString();
            }

            var envBlock = new EnvironmentBlock().Merge(typedDictionary);

            return(envBlock);
        }
        public static EnvironmentBlock Create(IDictionary dictionary)
        {
            Dictionary<string, string> typedDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var key in dictionary.Keys)
            {
                typedDictionary[key.ToString()] = dictionary[key].ToString();
            }

            var envBlock = new EnvironmentBlock().Merge(typedDictionary);

            return envBlock;
        }
Example #6
0
        public static EnvironmentBlock CreateForUser(IntPtr userToken)
        {
            var defaultEnvBlock = CreateEnvBlock(userToken);
            var environment     = new EnvironmentBlock();

            foreach (var key in ForbiddenEnvironmentVariables)
            {
                defaultEnvBlock.Remove(key);
            }

            environment.Merge(defaultEnvBlock);

            return(environment);
        }
        public static EnvironmentBlock CreateForUser(IntPtr userToken)
        {
            var defaultEnvBlock = CreateEnvBlock(userToken);
            var environment = new EnvironmentBlock();

            foreach (var key in ForbiddenEnvironmentVariables)
            {
                defaultEnvBlock.Remove(key);
            }

            environment.Merge(defaultEnvBlock);

            return environment;
        }
        public void GeneratesDefaultEnvironment()
        {
            var env  = EnvironmentBlock.CreateSystemDefault();
            var dict = env.ToDictionary();

            Assert.True(dict.Count > 0);
            // Verify some of the environment variables we expect to be there by default
            Assert.Contains("TEMP", dict.Keys);
            Assert.Contains("TMP", dict.Keys);
            Assert.Contains("SystemRoot", dict.Keys);

            foreach (var key in EnvironmentBlock.ForbiddenEnvironmentVariables)
            {
                Assert.DoesNotContain(key, dict.Keys);
            }
        }
Example #9
0
        /// <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());
        }
Example #10
0
 public EnvironmentBlock Merge(EnvironmentBlock envBlock)
 {
     return(this.Merge(envBlock._environment));
 }
 public EnvironmentBlock Merge(EnvironmentBlock envBlock)
 {
     return this.Merge(envBlock._environment);
 }