2011-09-27 23 views
0

我創建了一個用於承載WCF服務的唯一目的的Windows服務。在WCF服務中,我有一種方法,當客戶端訪問時讀取註冊表中的文件路徑。然後該方法讀取文件的內容併發回內容。Windows服務託管的WCF服務在嘗試讀取文件時獲取「訪問被拒絕」

對文件正在存儲(並隨後讀取)的業務案例,它不在WCF服務所在的目錄中。當試圖訪問文件時,我得到一個訪問被拒絕的錯誤。

我認爲最簡單的解決方案是更改我的Windows服務運行的帳戶。我將帳號更改爲本地服務和網絡服務。我爲這些憑證提供了有關該目錄的完全安全權限,但仍然無法讀取該文件!

我的下一個想法是,因爲這些機器在同一個域上,我可以使用模擬來讀取文件。我已經嘗試過這種設置,但現在我的客戶端無法找到我的WCF服務。

我的服務配置出現問題,導致我無法讀取文件或訪問服務?

這裏是我的App.config中從服務:

<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="TransportCredWSBinding"> 
      <security mode="Transport"> 
      <transport clientCredentialType="Windows" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 

    <services> 
     <service name="AMS.CRSS.Service"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="https://localhost:8000/CRSS/service"/> 
      <add baseAddress="http://localhost:8000/CRSS/service"/> 
      </baseAddresses> 
     </host> 
     <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/CRSS/service --> 
     <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TransportCredWSBinding" contract="AMS.Core.Services.IService"/> 
     <!-- the mex endpoint is exposed at https://localhost:8000/CRSS/service/mex --> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true--> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="False" httpsHelpPageEnabled="true"/> 
      <serviceCredentials> 
      <clientCertificate> 
       <authentication mapClientCertificateToWindowsAccount="true" /> 
      </clientCertificate> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    </system.serviceModel> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
</configuration> 

我的想法是,WCF服務正在不同於Windows的服務,這是它的主機不同的憑據運行。我錯了嗎?我想我可以嘗試給「每個人」訪問該文件夾,看看是否可行。

編輯:這裏是,我使用連接到該服務的客戶端代碼:

public static CRSSClient GetClientInstance(string clientHost) 
    { 
     UriBuilder ub = new UriBuilder("https", clientHost, 8000); 
     ub.Path = "CRSS/service"; 

     WSHttpBinding binding = new WSHttpBinding(); 
     binding.Security.Mode = SecurityMode.Transport; 
     binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; 

     CRSSClient client = new CRSSClient(binding, new EndpointAddress(ub.Uri)); 
     client.ClientCredentials.Windows.AllowedImpersonationLevel = 
      System.Security.Principal.TokenImpersonationLevel.Impersonation; 

     return client; 
    } 

在這裏是我從客戶端調用方法:

[OperationBehavior(Impersonation = ImpersonationOption.Required)] 
    public string ReadCAMConfig() 
    { 
     string camConfigText = null; 
     string camConfigFileLocation = GetCAMConfigLocationFromReistry(); 

     EventLog.WriteEntry("CRSS", "Reading CAM File: " + camConfigFileLocation + CAM_FILENAME); 

     WindowsIdentity identity = ServiceSecurityContext.Current.WindowsIdentity; 
     using (identity.Impersonate()) 
     { 
      try 
      { 
       if (File.Exists(camConfigFileLocation + CAM_FILENAME)) 
       { 
        camConfigText = File.ReadAllText(camConfigFileLocation + CAM_FILENAME); 
       } 
      } 
      catch (Exception e) 
      { 
       EventLog.WriteEntry("CRSS", e.ToOutputString(), EventLogEntryType.Error); 
      } 
     } 

     return camConfigText; 
    } 

回答

0

當調試一個您的服務電話,WindowsIdentity.GetCurrent()返回給您?

我想你在這裏有一個模擬問題;如果它是而不是服務帳戶,那麼您已經爲操作/服務啓用了模擬(請注意,客戶端設置不指定模擬,客戶端&服務器必須匹配),在這種情況下您必須授予全部您正在模擬的用戶。

你可能只是表明你想識別用戶,但不能完全模仿。

如果用戶您的服務帳戶,那麼您必須仔細檢查您嘗試爲您的服務正在運行的帳戶打開的文件的有效權限。

+0

這是事情,與上述配置,我甚至無法獲得該服務。我收到一個錯誤,說我有一個配置不匹配,SOAP進程不喜歡它,並殺死連接。我沒有確切的錯誤,因爲我回到使用標準的WsHttpBinding無安全地調用它。我試圖添加「Everyone」組到文件夾的權限,但我仍然得到:訪問路徑'C:\ CAMConfig'被拒絕。在System.IO .__錯誤時出現 。WinIOError(Int32 errorCode,String maybeFullPath) –

+0

@Mike G:您是否甚至嘗試查看當前Windows標識是什麼,如答案中所示? – casperOne

+0

我不確定如何破解服務中的代碼。我是否需要在服務器實例上安裝VS遠程工具? –

相關問題