2011-12-21 28 views
0

所以我一直在這一塊上敲我的頭。我有一個自我託管WCF服務:使用自託管WCF WebServiceHost的模擬獲取'發生操作錯誤'。錯誤

var webServiceHost = new WebServiceHost(helloWorld); 
webServiceHost.Authorization.ImpersonateCallerForAllOperations = true; 

var uri = new Uri(BaseUri + webService.UriDirectory); 
var webHttpBinding = new WebHttpBinding(webHttpSecurityMode); 
webHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; 

var sep = webServiceHost.AddServiceEndpoint(IHelloWorld, webHttpBinding, uri); 
var webHttpBehavior = new WebHttpBehavior {HelpEnabled = true}; 
sep.Behaviors.Add(webHttpBehavior); 

webServiceHost.Open(); 

我已經先行一步,並應用了以下屬性我的方法:

[OperationBehavior(Impersonation = ImpersonationOption.Required)] 
public List<GpoItem> GetAll() 
{ 
    using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate()) 
    { 
     // Execute GPO code here... 
     return new List<GpoItem>(); 
    } 
} 

要添加更多的情況下,我基本上有一個Web服務,它允許一個人登錄到網頁上,在域上創建一個GPO。在控制檯中運行它可以正常工作,因爲我以登錄的域用戶身份運行它。將它作爲Windows服務運行,會引發「拒絕訪問」異常。因此需要模仿。我在上面添加了以下更改後的代碼,並且收到'發生操作錯誤。 (來自HRESULT的異常:0x80072020)'。谷歌搜索顯示我仍然有權限問題。我以管理員身份登錄到Web服務測試環境,因此我具有完全訪問權限,並且我已經顯示我可以以管理員身份在控制檯中運行它。我覺得我錯過了一些國旗設置在哪裏。

任何想法?

[Update1]我試過將服務從本地系統切換到網絡服務,但我仍然遇到同樣的問題。

[更新2]當我登錄到託管我的WCF服務(作爲本地系統運行)的服務器,並直接在該機器上使用瀏覽器時,一切正常。這似乎是委託用戶身份驗證的問題......在這裏仍然未知。

+0

的'WindowsIdentity.Impersonate()'方法返回您沒有在'using'塊引用'WindowsImpersonationContext'值。當它超出範圍時,引用它會自動調用它的'WindowsImpersonationContext.Undo()'方法。 – Bernard 2011-12-21 17:43:13

+0

您是否嘗試過跟蹤您的WCF服務以查看引發異常的位置? – Bernard 2011-12-21 17:48:03

+0

我引發的異常是'System.Runtime.InteropServices.COMException(0x80072020):發生操作錯誤。 (來自HRESULT的異常:0x80072020)' – ymerej 2011-12-21 18:22:31

回答