1

我正致力於保護RESTFul服務。如何使用Windows身份驗證和註冊自定義身份驗證管理器

所有客戶端都是Windows Intranet客戶端,我想使用Windows集成身份驗證。作爲初始實現,我希望對最終用戶的憑據執行授權檢查,併爲用戶提供對服務「讀取」方法的訪問權限,並允許訪問服務帳戶上的寫入方法。我正在經歷許多不同的問題,以使這些工作得以順利進行。我已經在各個地方讀了大量的關節,但是迄今爲止還沒有流行。 以下是應用程序的web.config中的配置。

<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="Windows" /> 
    <identity impersonate="true" /> 
    </system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    </system.webServer> 
    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding" /> 
    </protocolMapping> 
    <services> 
     <service name ="SecureREST" behaviorConfiguration="RESTAuthorisationBehaviour"> 
     <endpoint address="" binding="webHttpBinding" bindingConfiguration="default" contract="Service.ISecureService"> 
     </endpoint> 
     </service> 
    </services> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="default"> 
      <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Ntlm" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="RESTAuthorisationBehaviour"> 
      <serviceAuthorization serviceAuthorizationManagerType="Security.RESTAuthorisationManager"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

在IIS中,我禁用了匿名身份驗證。我需要這樣做,因爲我們希望所有的客戶端都被認證,以便我們可以執行授權檢查。我還啓用了Windows身份驗證(NTLM?)。

的第一個問題我已經是當我測試這個從瀏覽器中,我得到了以下錯誤消息:

此服務的安全設置需要「匿名」身份驗證,但它不是爲IIS應用程序啓用承載此服務。

我不想啓用匿名身份驗證,因此看起來有一些配置在其他地方,表明匿名身份驗證,這顯然與我禁用它的事實不一致。我還需要指定哪些匿名身份驗證不被允許? (你也會看到我沒有使用任何根據各種文章可能導致此問題的mex端點)。

我的下一個問題是,我正在嘗試註冊一個自定義授權管理器。這樣做的原因是我想檢查用戶是哪個組的成員,並據此作出授權決定。例如,如果用戶是名爲「ReadOnly」的組的成員,那麼他們只能調用服務上的get方法。如果客戶端是「ReadAndWrite」組的成員,那麼他們也可以在服務上調用put方法。

你應該看到我試圖註冊授權類(從ServiceauthorisationManager派生);這是在RESTAuthorisationBehaviour行爲中表達的。我已經把一些日誌記錄到這個類中,但是這些都沒有顯示在日誌中,所以我知道它沒有正確註冊。

最後一個問題是模仿。爲了測試這個,在服務代碼中,我訪問WindowIdentity並在日誌中顯示它。我發現的是,用戶名並不代表最終用戶,這是我需要的,所以我可以執行前面描述的授權檢查。相反,我所看到的是用戶是應用程序池標識('IIS APPPOOL \ SecureREST')。這對我毫無用處。我打開了impersonate標誌(正如你可以從配置中看到的那樣),但是如何讓它正常工作,以便真正的客戶端憑證在WindowsIdentity對象中表示?我使用獲得Windows身份持有的代碼如下:

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent(); 
string accountName = windowsIdentity.Name; 

我也曾嘗試調用windowsIndentity對象無濟於事的冒充方法。

那麼我該如何冒充認證用戶呢?

回答

0

至於有關匿名身份驗證之外,嘗試禁用您的服務行爲中的HTTP GET的元數據,像這樣:

<behavior name="MyServiceBehavior"> 
     <serviceMetadata httpGetEnabled="false" /> 
    </behavior>