2010-09-22 113 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