0

我在从我的 Web 服务访问某些(但不是全部)注册表项时遇到了困难。因此,我假设(并通过一些研究证实)访问注册表存在一些安全限制。我需要在我的 C#.Net 应用程序中专门执行的配置中是否有一些代码或更改?

具体来说,我正在尝试在“Software\Microsoft\Internet Explorer\PageSetup”下读取和写入 PageSetup 的值

4

2 回答 2

0

冒充用户后HKEY_CURRENT_USER不会改变。您应该在模拟用户和RegCloseKey之后使用RegOpenCurrentUser

或者,您获取用户的 SID 并从以下位置读取注册表HKEY_USERS

WindowsIdentity wi = HttpContext.Current.User.Identity as WindowsIdentity;
if (windowsIdentity != null) {
    SecurityIdentifier si = wi.User;
    RegistryKey key = Registry.Users.OpenSubKey (si.Value +
                            @"\Software\Microsoft\Internet Explorer\PageSetup");
    // get some values which you need like
    string top_margine = key.GetValue ("margin_top");
    key.Close();
}
于 2010-09-22T08:05:19.960 回答
0

您可以使用 System.Security.Principal.WindowsIdentity.GetCurrent() 创建一个 Web 方法,该方法返回当前用户的名称(很可能是特殊的 ASP_NET 用户),然后增加用户的权限(或更改安全设置您要从 regedit 编辑的键,以便运行您的进程的用户能够读取注册表的部分

另一方面,如果我是对的,并且您想要编辑 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup,并且您的目标不是为 ASP_NET 用户更改该密钥中的信息,那么您需要对您的webservice 使用服务器机器中可用的帐户,为此,您需要将 webservice 配置为在 Web.config 中使用 windows 身份验证:

<system.web> ... <authentication mode="Windows"/> <identity impersonate="true"/> ... </system.web>

然后获取经过身份验证的用户的 Windows 令牌:


IIdentity WinId= HttpContext.Current.User.Identity;
WindowsIdentity wi = (WindowsIdentity)WinId;

最后,您使用经过身份验证的用户的 Windows 令牌临时模拟原始用户,并在完成模拟后从当前线程中删除模拟令牌。


// Temporarily impersonate the original user.
WindowsImpersonationContext wic = wi.Impersonate();
try
{
  // Access resources while impersonating.
}
finally
{
  // Revert impersonation.
  wic.Undo();
}

这样,当您要求 WindowsIdentity.GetCurrent() 时,您将获得要进行身份验证的 Windows 帐户用户的名称(这称为临时模拟经过身份验证的用户)。您将有权访问您用于身份验证的用户的 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

此处有关 Windows 身份验证和模拟的更多信息:http: //msdn.microsoft.com/en-us/library/ff647405.aspx

于 2010-09-22T00:43:02.773 回答