我从Web服务访问某些(但不是全部)注册表项时遇到了困难。因此,我假设(并通过一些研究证实)访问注册表存在一些安全限制。我需要在C#.Net应用程序中专门做一些代码或更改吗?
具体来说,我试图在“Software \ Microsoft \ Internet Explorer \ PageSetup”下读取和写入PageSetup的值
答案 0 :(得分:0)
您可以使用System.Security.Principal.WindowsIdentity.GetCurrent()创建一个Web方法,返回当前用户的名称(很可能是特殊的ASP_NET用户),然后增加用户的权限(或更改要从regedit编辑的密钥的安全设置,以便运行进程的用户能够读取注册表的一部分
另一方面,如果我是对的,并且您想要编辑HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ PageSetup,并且您的目标不是更改ASP_NET用户的该键中的信息,那么将需要使用服务器计算机中可用的帐户对您的Web服务进行身份验证,为此,您需要配置web服务以在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
答案 1 :(得分:0)
模仿用户HKEY_CURRENT_USER
后,不会更改。您应该在模拟用户和RegOpenCurrentUser之后使用RegCloseKey。
或者,您可以从HKEY_USERS
获取用户的SID并阅读注册表:
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();
}