【问题标题】:C# released exe won't start on non-admin accountsC# 发布的 exe 不会在非管理员帐户上启动
【发布时间】:2019-10-09 19:40:22
【问题描述】:

所以我创建了一个 GUI,它使用模拟和 powershell 命令激活和停用网络适配器。

这是代码中的一个sn-p:

Get_adapters 获取所有适配器并将它们绑定到列表视图:

    public MainWindow()
    {
        InitializeComponent();
        Get_adapters();
    }

这是我的两个按钮命令:

    private void Activate_interface(object sender, RoutedEventArgs e)
    {
        string interface_name = ((Netadapter)ListView.SelectedItem).Name;
        Change_adapter_status(true, interface_name);
    }

    private void Deactivate_interface(object sender, RoutedEventArgs e)
    {
        string interface_name = ((Netadapter)ListView.SelectedItem).Name;
        Change_adapter_status(false, interface_name);
    }

这是 powershell 方法:(隐藏了帐户凭据;但我已经检查了多次,它们都是正确的)。

    public void Change_adapter_status(bool _option, string _interface)
    {
        using (new Impersonator("Administrator", "DOMAIN", "pw"))
        {
            var process_info = new ProcessStartInfo();
            process_info.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
            process_info.Verb = "runas";

            if (_option == true)
            {
                process_info.Arguments = String.Format("netsh interface set interface „{0}“ enable", _interface);
            }
            else if (_option == false)
            {
                process_info.Arguments = String.Format("netsh interface set interface „{0}“ disable", _interface);
            }
            Process.Start(process_info);
        }
    }

如您所见,我使用模拟以管理员用户身份启动 powershell 窗口。

在我的机器上 (Admin acc) 一切正常。单击按钮,powershell 窗口打开并执行其操作。

在其他计算机上(非管理员但在同一个域中,并且可以使用上面代码中的凭据访问管理员帐户),exe 甚至都不会启动。

计算机具有相同的图像(Windows 10)并且目标 .net 框架也是正确的。

在其他管理员帐户上,exe 是可执行的。那么它可能是什么 exe不会为非管理员用户启动?提前谢谢。

事件输出: - 系统

  • 提供者

    [名称] .NET 运行时

  • 事件 ID 1026

    [限定符] 0

    2级

    任务 0

    关键字 0x80000000000000

  • 创建时间

    [系统时间] 2019-05-23T10:01:14.748998500Z

    事件记录 ID 6679

    渠道申请

    计算机 PLMC12906.greencorps.intra

    安全

    • 事件数据

    应用程序:Adapter.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。获取信息:System.Runtime.InteropServices.COMException 来自 System.Management.MTAHelper.CreateInMTA(System.Type) 来自 System.Management.ManagementPath.CreateWbemPath(System.String) 来自 System.Management.ManagementPath..cctor() TypeInitializationException bei System.Management.ManagementScope._Clone(System.Management.ManagementScope, System.Management.IdentifierChangedEventHandler) bei System.Management.ManagementObjectSearcher..ctor(System.Management.ManagementScope, System.Management.ObjectQuery, System.Management.EnumerationOptions) bei System.Management.ManagementObjectSearcher..ctor(System.Management.ObjectQuery) bei Adapter.MainWindow.Get_adapters() bei Adapter.MainWindow..ctor() Ausnahmeinformationen: System.Windows.Markup.XamlParseException bei System.Windows.Markup.WpfXamlLoader .Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri) 在 System.Windows.Ma rkup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri) 在 System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows .Markup.ParserContext, System.Object, Boolean) bei System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext) bei System.Windows.Application.LoadComponent(System.Uri, Boolean) bei System.Windows.Application.DoStartup() 贝 System.Windows.Application.<.ctor>b__1_0(System.Object) 贝 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 贝 系统。 Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 来自 System.Windows.Threading.DispatcherOperation.InvokeImpl() 来自 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System .Object) bei MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object) b ei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 北 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System. Object, Boolean) bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) bei MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) 贝 System.Windows.Threading.DispatcherOperation.Invoke() 贝 System.Windows.Threading.Dispatcher.ProcessQueue() 贝 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)在 MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System. ○ bject, Int32) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading. DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef ) 北 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 北 System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) 北 System.Windows.Application.RunDispatcher(System.对象)bei System.Windows.Application.RunInternal(System.Windows.Window) bei System.Windows.Application.Run(System.Windows.Window) bei System.Windows.Application.Run() bei Adapter.App.Main()

【问题讨论】:

    标签: c# admin impersonation


    【解决方案1】:

    尝试将 app.manifest 文件添加到您的可执行文件的项目中,并在其上设置&lt;requestedExecutionLevel level="requireAdministrator" uiAccess="false" /&gt;

    【讨论】:

    • 添加了 app.manifest 并将 asInvoker 替换为 requireAdministrator。不幸的是,我得到了相同的输出。系统尝试启动(光标加载)但没有任何反应。
    • 您检查过事件日志吗?它有什么帮助吗?尝试检查此thread 并修改模拟
    • 我已经编辑了我的问题并 eidtet 事件日志。这是一个运行时错误。我正在尝试您现在发送的线程中的解决方案,如果它有效,我会更新。感谢您的链接!
    • @Cross 有链接同样异常here 问题可以在C:\Windows\SysWOW64路径硬编码
    【解决方案2】:

    您必须使用WindowsIdentity.Impersonate Method 来模拟。

    【讨论】:

      【解决方案3】:

      所以这就是我最终这样做的方式: 我删除了代码的模拟部分,并将其替换为 Process.StartInfo(); 中的 inbuild 方法; 这是为非管理员用户配置网络适配器的 gui 的一部分。随意使用。

      public void Change_adapter_status(bool _option, string _interface)
              {
                  SecureString password = new SecureString();
                  password.AppendChar('p');
                  password.AppendChar('a');
                  password.AppendChar('s');
                  password.AppendChar('s');
                  password.AppendChar('w');
                  password.AppendChar('o');
                  password.AppendChar('r');
                  password.AppendChar('d');
      
                  Process p = new Process();
                  p.StartInfo.UserName = "Administrator";
                  p.StartInfo.Domain = "domain";
                  p.StartInfo.Password = password;
                  p.StartInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
                  p.StartInfo.Verb = "runas";
                  p.StartInfo.UseShellExecute = false;
      
                  if (_option == true)
                  {
                      p.StartInfo.Arguments = String.Format("netsh interface set interface „{0}“ enable", _interface);
                  }
                  else if (_option == false)
                  {
                      p.StartInfo.Arguments = String.Format("netsh interface set interface „{0}“ disable", _interface);
                  }
                  p.Start();
                  p.WaitForExit();
                  Get_adapters(true);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 2013-12-13
        • 2013-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多