简体   繁体   English

C# WPF 应用程序权限问题

[英]C# WPF application permissions questions

I'm writing a C# WPF application that:我正在编写一个 C# WPF 应用程序:

  1. Collects hardware information through WMI (with ManagementObjectSearcher )通过 WMI 收集硬件信息(使用ManagementObjectSearcher
  2. Captures processes' ETW draw events to measure FPS (with TraceEventSession )捕获进程的 ETW 绘制事件以测量 FPS(使用TraceEventSession

I use Setup Project 's installer to distribute the app.我使用Setup Project的安装程序来分发应用程序。 But, after installing on another computer, exe only opens with admin rights.但是,在另一台计算机上安装后,exe 只能以管理员权限打开。

Google didn't help me to find the answer to:谷歌没有帮助我找到答案:

  1. Why does my app runs without permission on my PC?为什么我的应用程序未经许可在我的 PC 上运行?
  2. Which task requires permissions and how should I know about it?哪个任务需要权限,我应该如何知道?
  3. Is there a way to solve the problem without permission?有没有办法在未经许可的情况下解决问题?
  4. Why doesn't my app ask for permissions as other apps do?为什么我的应用不像其他应用那样请求权限?

I would greatly appreciate an answer to one or all of these questions!我将不胜感激对其中一个或所有问题的回答! Thanks.谢谢。

Many operations that involve the WMI management interfaces require admin rights, that's just a fact.许多涉及 WMI 管理接口的操作都需要管理员权限,这是事实。 You didn't exactly specify what you do there, but querying some hardware information is likely to require this.您没有具体说明您在那里做什么,但查询一些硬件信息可能需要这样做。

To test what call exactly requires admin rights, run the app on your computer without admin rights and check which call fails.要测试哪些调用需要管理员权限,请在没有管理员权限的计算机上运行该应用程序并检查哪个调用失败。 You can even debug your app in that mode (just make sure you don't start Visual Studio as admin).您甚至可以在该模式下调试您的应用程序(只需确保您没有以管理员身份启动 Visual Studio)。

To make your app require admin rights, create an application manifest file (Use the "Application Manifest" template in the "new file" window) and add a line requestedExecutionLevel below the <requestedPrivileges> node as follows:要使您的应用程序需要管理员权限,请创建一个应用程序清单文件(使用“新文件”窗口中的“应用程序清单”模板)并在<requestedPrivileges>节点下方添加一行requestedExecutionLevel ,如下所示:

      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>

This will result in Windows asking for admin rights each time the App is started.这将导致 Windows 每次启动应用程序时都要求管理员权限。 This is a global setting for the whole application.这是整个应用程序的全局设置。 It is not possible to ask for the permission only if you need it.仅在您需要时才请求许可是不可能的。

I have this problem and I know why and why your program does not recognize Permission and has free access.我有这个问题,我知道为什么以及为什么您的程序无法识别权限并且可以免费访问。 Even considering that you configured the code correctly.即使考虑到您正确配置了代码。 Well, my friend, this is because you have to write your permutation class on the controller inside the bracket as a property and let the controller of that page know that this page is in Access to who should be with the same property perm checker.好吧,我的朋友,这是因为您必须在括号内的控制器上编写您的置换类作为属性,并让该页面的控制器知道该页面在访问谁应该使用相同的属性 perm 检查器。 [1=Permission Check] [1=权限检查]

[PermissionCheck="admin"] [PermissionCheck="管理员"]

public class ProductControllers:Controller公共类 ProductControllers:Controller

  public IActionResult index
 
         return view
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM