.Net Core 生成管理员权限的应用程序

server/2024/9/24 22:31:31/
  • 创建一个ASP.NET Core Web API项目

  • 给解决方案设置一个名称

  • 选择一个目标框架,这里选择的是 .NET 8.0框架

  • 在Porperties文件夹中添加一个app.manifest文件

  • 设置app.manifest文件属性,生成操作设置为嵌入的资源

双击解决方案名称,编辑WebApplication22.csproj文件,在.csproj文件中加入一行代码

<ApplicationManifest>Properties\app.manifest</ApplicationManifest>

.csproj配置文件如下 

<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net8.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings><ApplicationManifest>Properties\app.manifest</ApplicationManifest></PropertyGroup><ItemGroup><EmbeddedResource Include="Properties\app.manifest" /></ItemGroup><ItemGroup><PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /></ItemGroup>
</Project>

app.manifest文件配置如下

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"><assemblyIdentity version="1.0.0.0" name="MyApplication.app" /><trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"><security><requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"><requestedExecutionLevel level="requireAdministrator" uiAccess="false" /></requestedPrivileges><applicationRequestMinimum><defaultAssemblyRequest permissionSetReference="Custom" /><PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" /></applicationRequestMinimum></security></trustInfo><compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"><application></application></compatibility>
</assembly>

需要注意的是,需要把requestedExecutionLevel节点中的level值设置为“requireAdministrator”

  • 此时重新生成解决方案发现我们的exe程序就会有一个盾牌的标识,说明是成功设置为了管理员身份启动。

  • 另外我们可以在Program.cs文件中的Main方法中加入如下代码,用于判断程序是否以管理员身份运行:
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
if (principal.IsInRole(WindowsBuiltInRole.Administrator)) //必须是管理员身份运行
{//CreateHostBuilder(args).Build().Run();
}

附:

参考网址【搞懂.NET应用程序管理员权限:三种简单方法一网打尽_net 权限管理-CSDN博客】


http://www.ppmy.cn/server/121535.html

相关文章

[ffmpeg] packet

API调用 常用API AVPacket *av_packet_alloc(void); // 构建 packet 结构体 void av_packet_free(AVPacket **pkt); // 释放 packet 结构体 void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst); // pts,dts,duration 基于前后时间基进行转换结…

手写流程图元素检测系统源码分享

手写流程图元素检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Comput…

[Golang] Context

[Golang] Context 文章目录 [Golang] Context什么是context创建context创建根context创建context context的作用并发控制context.WithCancelcontext.WithDeadlinecontext.WithTimeoutcontext.WithValue 什么是context Golang在1.7版本中引入了一个标准库的接口context&#xf…

多颜色绘制语义分割/变化检测结果图

在论文绘图时&#xff0c;传统的二元语义分割结果图颜色单一&#xff08;下图左&#xff09;&#xff0c;所以论文中常根据混淆矩阵类别使用多颜色进行绘制&#xff08;下图右&#xff09;&#xff0c;可以看到&#xff0c;结果的可视化效果更好。 以下是绘制代码&#xff1a; …

结构设计模式 -装饰器设计模式 - JAVA

装饰器设计模式 一. 介绍二. 代码示例2.1 抽象构件&#xff08;Component&#xff09;角色2.2 具体构件&#xff08;Concrete Component&#xff09;角色2.3 装饰&#xff08;Decorator&#xff09;角色2.4 具体装饰&#xff08;Concrete Decorator&#xff09;角色2.5 测试 结…

数理统计(第一章)

数理统计核心问题&#xff1a;有子样推断母体 1.1 母体和子样 母体&#xff1a;研究对象的全体&#xff08;关心个体的一项或者几项&#xff09;数量指标及总体的分布情况。 比如&#xff1a;一批灯泡的使用寿命&#xff0c;班级学生的身高&#xff0c;体重等。 1.2 母体分布…

Spring MVC 基本配置步骤 总结

1.简介 本文记录Spring MVC基本项目拉起配置步骤。 2.步骤 在pom.xml中导入依赖&#xff1a; <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>6.0.6</version><scope>…

类型转换与RTTI

类型转换 一、类型转换1、C语言2、C3、注意 二、static_cast1、介绍2、示例 三、reinterpret_cast1、介绍2、示例代码3、运行结果 四、const_cast1、介绍2、示例 五、dynamic_cast1、介绍2、示例代码3、运行结果 六、RTTI1、介绍2、typeid&#xff08;1&#xff09;介绍&#x…