2012-05-31 53 views
2

我已經有一個網站和幾個項目的解決方案。
這些項目都具有AllowPartiallyTrustedCallers屬性,並且是強命名的。中等信任的LDAP

該網站完全信任。但是,將信任設置爲媒介後,只要我瀏覽到該網站,就會收到System.Security.SecurityException: Request failed. 錯誤。

在我的項目中,我撥打了LogOnUser,以及撥打各種System.DirectoryServices.AccountManagement方法的電話。

該網站可以以中等信任度運行嗎?還是必須完全信任所有LDAP呼叫?

正如我所提到的,我已經爲所有項目設置了AllowPartiallyTrustedCallers屬性。不知道還有什麼要做。
此外,我不知道什麼/哪裏錯誤正在生成。事件日誌在服務器上沒有任何關於這個SecurityException。有什麼辦法找出錯誤的位置是什麼,也許我可以嘗試重寫一些代碼?

[上Win2k8R2運行.NET 4.0]

+0

downvote的任何理由? – eych

+0

什麼是LogOnUser?您的'AllowPartiallyTrustedCallers'程序集在您的bin目錄或GAC中? –

+0

'LogOnUser'是位於advapi32.dll中的Win32函數。它用於調用Active Directory。我的問題是關於能夠在中等信任的情況下調用Win32 dll函數。另外,我的程序集不在GAC中,只是bin。 – eych

回答

4

LogOnUser,像所有的P/Invoke調用,需要SecurityPermissionUnmanagedCode許可標誌。 System.DirectoryServices.AccountManagement要求不受限制DirectoryServicesPermission。默認情況下,不授予中等信任ASP.NET應用程序的權限。

AllowPartiallyTrustedCallers屬性允許部分信任程序集使用完全信任的程序集。在你的情況下,該屬性不起作用,因爲bin文件夾中的所有程序集都被加載到部分信任的應用程序域中。

如果您的應用程序需要在中等信任下運行,並且您有能力將程序集安裝到GAC中,則可以創建包含需要額外權限的代碼的程序集,並使用AllowPartiallyTrustedCallers標記程序集並將其放入在GAC中。您還需要Assert所需的權限來抑制仍會發生的堆棧走動。

有關詳細信息,請參閱MSDN Library中的Code Access Security in ASP.NET 4 Applications主題。

0

雖然@Michael Liu的回答是正確的,但是這個領域的工作知識有限的新手(比如我自己)很難理解。這個答案是爲了補充邁克爾的答案。

當你的web.config配置爲使用的信任級別是什麼,其他「滿」

<trust level="Full"/> 

,那麼你將無法連接到Active Directory沒有creating your own policy file。一旦你創建了自己的策略文件(假設它被稱爲「myPolicyFile.config」),那麼你就可以自定義它來讓你的ASP.NET應用程序連接到Active Directory。

下面是你需要做的修改:

在web.config中,配置網站使用自定義策略文件:

<system.web> 
    ... 
    <securityPolicy> 
     <trustLevel name="myMediumPolicy" policyFile="myPolicyFile.config"/> 
    </securityPolicy> 

    <trust level="myMediumPolicy"/> 
    ... 
</system.web> 

接下來,在你的myPolicyFile。配置文件,添加「SecurityClass」和「IPermission」條目顯示在下面:

<configuration> 
    ... 
     <PolicyLevel version="1"> 
      <SecurityClasses> 
       .... 
       <SecurityClass Name="DirectoryServicesPermission" Description="System.DirectoryServices.DirectoryServicesPermission, System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
       .... 
      </SecurityClasses> 
      <NamedPermissionSets> 
       <PermissionSet 
         class="NamedPermissionSet" 
         version="1" 
         Name="ASP.Net"> 
        ... 
        <IPermission 
          class="DirectoryServicesPermission" 
          version="1" 
          Unrestricted="true" 
        /> 
        <IPermission 
          class="SecurityPermission" 
          version="1" 
          Flags="Execution, ControlThread, ControlPrincipal, RemotingConfiguration, UnmanagedCode" 
         /> 
        ... 
       </PermissionSet> 
      </NamedPermissionSets> 
     </PolicyLevel> 
    ... 
</configuration> 

注:< IPermission類=「的SecurityPermission」 .../>節點可能已經存在(取決於系統定義了基於myPolicyFile.config文件的信任級文件)。如果它已經存在,那麼你只需要添加文本「,UnmanagedCode」到「標誌」屬性。如果它尚不存在,則複製上面的示例並將其粘貼到「ASP.Net」NamedPermissionSet中。

最後,保存您的更改並重新加載您的網站。你應該很好走!