C#Windows服务使用HKEY_USERS当前登录的用户 [英] c# windows service use HKEY_USERS current logged user

查看:230
本文介绍了C#Windows服务使用HKEY_USERS当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为LocalSystem运行的Windows服务,我需要从登录的当前用户访问regedit。我尝试使用以下方法:
System.Security.Principal.WindowsIdentity.GetCurrent()。Name ->返回系统配置文件(S-1-5-18),我需要是 S-1-5-21-2238971182-363666278-428364487-1001;
Environment.UserName ,它返回系统。
我也使用CurrentUser但还是一样。^
如何访问系统使用但与当前有关的重载密钥登录的SID

I have a windows service running as LocalSystem and i need to access a regedit from the current user logged. I try work around using: System.Security.Principal.WindowsIdentity.GetCurrent().Name -> that returns a profile of System (S-1-5-18) and i need is "S-1-5-21-2238971182-363666278-428364487-1001"; Environment.UserName it returns "System" I also use CurrentUser but is the same.^ How i access a regestry key used by System but with the current logged use SID

推荐答案

一次在Windows中可以是多个登录用户。因此需要一些条件,根据该条件选择当前。例如,您可以调用 WTSGetActiveConsoleSessionId ,然后在调用 WTSQueryUserToken 。或者,您可以通过 <$来枚举所有当前登录的用户。 c $ c> WTSEnumerateSessions ,并针对每个调用 WTSQueryUserToken

in windows at once can be multiple logged-in users. so need some condition, based on which you select "current". for example you can call WTSGetActiveConsoleSessionId and then use this SessionId in call WTSQueryUserToken. alternatively you can enumerate all currently logged users by WTSEnumerateSessions and for each call WTSQueryUserToken

(在获得用户令牌后,这将是 TokenPrimary TOKEN_TYPE ),则可以 TokenUser 获取用户 SID ,然后使用此 SID 来打开 HKEY_USERS\ {sid} 键(这是最有效的)。或致电 DuplicateToken 获取 TokenImpersonation SetThreadToken 和< a href = https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724894(v=vs.85).aspx rel = nofollow noreferrer> RegOpenCurrentUser (此后再次调用 SetThreadToken(0,0)进行重置模拟)-但这当然不如第一种方法有效

after you got user token (this will be TokenPrimary TOKEN_TYPE), you can query it TokenUser for get user SID and then use this SID for open HKEY_USERS\{sid} key (this is most effective). or call DuplicateToken for get TokenImpersonation, SetThreadToken and RegOpenCurrentUser (after this again call SetThreadToken(0,0) for reset impersonation) - but this is of course much less effective then first way.

这篇关于C#Windows服务使用HKEY_USERS当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆