模拟Windows服务中的登录用户 [英] impersonating logged on user in windows service

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

问题描述

我有一个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的LogonUser API调用,则可以为任何用户获得令牌.
The service itself does not have any direct access to the user logged. You can however get a token for any user if you use the LogonUser API call to windows.


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

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