2010-09-22 160 views
0

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

具体来说,我想“软件\微软\的Internet Explorer \ PAGESETUP”

+0

重点软件\微软\的Internet Explorer \ PAGESETUP是......哪里?在HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ PageSetup?那么这意味着您想要访问特定用户的PageSetup?哪个用户? – Luxspes 2010-09-22 00:43:51

+0

是的,我试图从HKEY_CURRENT_USER获取PageSetup。我想我可以创建一个通用用户帐户 – marcwenger 2010-09-22 05:12:24

回答

0

用户HKEY_CURRENT_USER的模拟后,将没有改变。模拟用户后应使用RegOpenCurrentUserRegCloseKey

另外,您得到了用户的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(); 
} 
0

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

另一方面,如果我是正确的,并且您想编辑HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ Page安装程序,您的目标不是为ASP_NET用户更改该密钥中的信息,然后需要使用服务器计算机中可用的帐户向您的web服务进行身份验证,为此,您需要将web服务配置为使用Windows在Web.config文件验证:

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

然后你获得验证用户的Windows标记:

 

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

最后你使用验证用户的Windows令牌临时模拟原始用户并删除冒充来自当前thr的令牌当你完成模拟时,你会发现。

 

// 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 \软件\微软\的Internet Explorer \ PAGESETUP你用来验证

在Windows身份验证和模拟这里更多信息:http://msdn.microsoft.com/en-us/library/ff647405.aspx

+0

我创建了模拟成功(为了测试目的,我模拟了我的用户名,它具有注册表访问权限,并且具有设置或PageSetup。但是,我仍然没有得到很多密钥。如果我查看HKCU \ Software \ Microsoft \ Internet Explorer \ Main(对于标题),那么我只能看到值名称:NoUpdateCheck,NoJITSetup,Disable Setup Script。这是否是不正当模仿的结果? – marcwenger 2010-09-22 05:20:38

+0

很多! – marcwenger 2010-09-22 18:58:08