c# - 在没有管理员权限的情况下同步时间 wint NTP 服务器
问题描述
我写了一个应用程序,它从 NTP 服务器获取时间并更改我机器的系统时间。它工作得很好,但只有当我的应用程序以管理员权限启动时才会同步时间。所以一个问题是如何在没有管理员权限的情况下启动它,但要保存它的功能?我使用 WinAPI 函数设置系统时间SetSystemTime
。
解决方案
您可以通过多种方式做到这一点(选择最适合您的一种)。
身份模拟 =>在此地址以示例进行描述
WindowsIdentity identity = new WindowsIdentity(accessToken); WindowsImpersonationContext context = identity.Impersonate() ;
runas 动词
ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath); startInfo.Verb = "runas"; System.Diagnostics.Process.Start (startInfo);
请记住,将提示 UAC 使用第二种方法
PrincipalPermissin(如果用户是 Admin 组的成员)
[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
推荐阅读
- c# - 如何合并一个被拆分为 2 个具有奇数和偶数索引的数组的数组?
- cropper - Cropper.js 以编程方式裁剪
- php - 使用 laravel 在代码中间结束一个 for 循环
- ansible - 无法使用 ansible 启动或停止 niginx
- python - Beautifulsoup 到 mysql
- java - 快速重新着色位图
- android - 将选择保留在 EditText 的底部
- angular - Angular5 中显示 TypeError 的两种方式绑定:无法读取 null 的属性“标题”
- asynchronous - 使用 Proc::Async 从绑定管道中读取
- regex - 正则表达式从数组中删除特定对象以及逗号(如果它跟在它后面)