C# 访问另一个用户的注册表 [英] C# Access registry of another user

查看:64
本文介绍了C# 访问另一个用户的注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的 Windows 服务有问题.基本上,我将一些值存储在 HKCU 注册表中(来自以管理员身份运行的 GUI 工具),并从该 GUI 中启动服务.该服务使用 SYSTEM 帐户运行,我相信这是我的问题 - 我无法访问使用我的 GUI 工具存储在服务内的注册表项,因为它指向不同的 HKCU!

I'm having a problem with the windows service I work on currently. Basically I store some values in HKCU registry (from a GUI tool run as administrator) and from within that GUI I am starting a service. The service uses SYSTEM account to run and I believe that's my problem - I can't access registry keys stored with my GUI tool inside the service, as it points to a different HKCU!

如何重定向"服务以使用存储它的用户的 HKCU?(实际上,如果有人会告诉我如何在我的 GUI 中检索它,我可以将用户名传递给服务或 SID,但我不知道我应该用什么来更改"用户以指向正确的用户名)

How can I "redirect" the service to use the HKCU of the user it was stored with? (Actually I can pass a user name to the service or SID if someone will point me how to retrieve it in my GUI, but I don't know what should I use to "change" the user to point to the correct one)

@编辑

我使用静态类来访问注册表,GUI 和服务都使用它,检索基键的函数是(rootKey 是保存子键名称的字符串变量):

I use a static class to access registry, it is used by both GUI and Service and the function to retrieve the base key is (rootKey is string variable holding the subkey name):

private static RegistryKey GetBaseKey(bool writable = false)
        {
            try
            {
                RegistryKey reg = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
                RegistryKey rk = reg?.OpenSubKey("SOFTWARE", writable)?.OpenSubKey(rootKey, writable);

                return rk;
            }
            catch (Exception ex)
            {
                // handle exceptions later
            }

            return null;
        }

我找到了可以为当前用户提供句柄 (AccessToken) 的 WindowsIdentity 类,我应该将它作为参数传递给我的服务并使用此句柄在服务内部模拟吗?

I have found WindowsIdentity class which can provide a handle (AccessToken) for current user, should I pass it as an argument to my service and use this handle to impersonate inside the service?

@EDIT2

我做了一些事情,但没有用.我试过的:

I have done some stuff but it doesn't work. What I tried:

CurrentUserToken = WindowsIdentity.GetCurrent().Token; // to get current identity token

然后用 ServiceController.Start 我添加了 CurrentUserToken.ToString()作为论据.在我的服务中,我使用传递的值初始化了 RegistryUserToken (IntPtr) 并且我被困在:

then with ServiceController.Start I added CurrentUserToken.ToString() as an argument. Within my service I initialized RegistryUserToken (IntPtr) with the passed value and I'm stuck at:

WindowsIdentity RegUser = new WindowsIdentity(RegistryUserToken)

抛出异常

用于模拟的令牌无效 - 无法复制

Invalid token for impersonation - it cannot be duplicated

我对当前 WindowsIdentity 实例的 AccessToken 进行了相同的尝试 - 抛出了相同的异常

I tried the same with AccessToken of current instance of WindowsIdentity - same exception thrown

我完全可以走那条路吗?或者我应该尝试不同的东西?

Can I at all go that way? Or should I try something different??

推荐答案

好的,我已经设法以不同的方式解决了这个问题.我已经将 SID 变量添加到我的 Registry 类中,如果它不为空,那么我打开用户注册表配置单元而不是 HKCU.首先,我使用以下方法检索当前的用户 SID(在我的 GUI 应用程序中):

Alright, I've managed to solve it going a bit different way. I've added SID variable to my Registry class and if it's not null then I open Users Registry Hive instead of HKCU. First I retrieve current's user SID (within my GUI application) using:

WindowsIdentity.GetCurrent().User.ToString();

我将其作为参数传递给我的服务并将其设置为 Registry 类.

which I pass as an argument to my service and set it for Registry class.

这篇关于C# 访问另一个用户的注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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