Exemplo n.º 1
0
        public void UndoImpersonationTest()
        {
            var target = new WindowsSecurityHelper();

            target.UndoImpersonation();
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
Exemplo n.º 2
0
        public void DisposeTest()
        {
            var target = new WindowsSecurityHelper();

            target.Dispose();
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Performs taks main actions.
        /// </summary>
        /// <param name="timer"></param>
        protected override void OnExecute(Timer timer)
        {
            using (var securityHelper = new WindowsSecurityHelper())
            {
                STrace.Trace(GetType().FullName, "Checking services status.");

                if (!securityHelper.ImpersonateValidUser())
                {
                    throw new Exception("Can not impersonate admin user for restarting a service.");
                }

                var crashedServices = GetCrasehdServices();

                if (!crashedServices.Any())
                {
                    STrace.Trace(GetType().FullName, "All services are running normally.");
                }
                else
                {
                    foreach (var winService in crashedServices)
                    {
                        RestartService(winService);
                    }
                }

                securityHelper.UndoImpersonation();
            }
        }
        public override string ParseProperty(SearchResult result)
        {
            byte[] binarySid = result.Properties[PropertyName][0] as byte[];
            string objectSID = WindowsSecurityHelper.ConvertByteToStringSid(binarySid);

            objectSID = PropertyKey(objectSID);
            return(objectSID);
        }
Exemplo n.º 5
0
        public void ImpersonateValidUserTest()
        {
            var        target   = new WindowsSecurityHelper();
            const bool expected = false;
            var        actual   = target.ImpersonateValidUser();

            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Change service status.
        /// </summary>
        /// <param name="item"></param>
        private void ServiceReact(string item)
        {
            using (var securityHelper = new WindowsSecurityHelper())
            {
                if (!securityHelper.ImpersonateValidUser())
                {
                    ThrowError("IMPERSONATION_FAILED");
                }

                var service = new ServiceController(item);

                switch (service.Status)
                {
                case ServiceControllerStatus.Running:
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                    break;

                case ServiceControllerStatus.StopPending:
                    //Procesos
                    string query    = string.Format("SELECT ProcessId FROM Win32_Service WHERE Name='{0}'", service.ServiceName);
                    var    searcher = new ManagementObjectSearcher(query);
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        var id      = ((int)obj["ProcessId"]);
                        var process = System.Diagnostics.Process.GetProcessById(id);
                        process.Kill();
                    }
                    break;

                case ServiceControllerStatus.Stopped:
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                    break;
                }

                Bind();

                securityHelper.UndoImpersonation();
            }
        }