问题描述
我有一个Windows服务,在本地系统帐户下运行.我需要执行一些登录"操作用户通过服务进行的特定操作(注册表中CurrentUser中的某项).我知道我需要在Windows服务中模拟已登录的用户,但很难创建Windows身份.我正在使用类似以下代码的内容
IntPtr logonToken = WindowsIdentity .GetCurrent().Token;
IntPtr logonToken = WindowsIdentity.GetCurrent().Token;
WindowsIdentity id = 新 WindowsIdentity (logonToken);
WindowsIdentity id = new WindowsIdentity(logonToken);
WindowsImpersonationContext impersonate = id.Impersonate();
WindowsImpersonationContext impersonate = id.Impersonate();
第一行给出了SYSTEM帐户,因为Windows服务在该上下文下运行,而不是已登录的用户-因此对我来说毫无用处.
我需要一些将loogedon用户令牌返回为IntPtr的方法.在这种情况下,我可以使用第1行.
否则我可以直接将第2行用作
WindowsIdentity id = 新 WindowsIdentity (用户主名称)
WindowsIdentity id = new WindowsIdentity(userprincipalname)
在这种情况下,我需要获取登录用户的主体名称,因为仅用户名就足够了.如何获取登录用户并为其找到主体名称.
谢谢,
Yuva
推荐答案
这篇关于模拟Windows服务中的登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!