【问题标题】:LDAP in medium trust中等信任的 LDAP
【发布时间】:2012-06-06 02:23:43
【问题描述】:

我有一个包含一个网站和多个项目的解决方案。
这些项目都具有AllowPartiallyTrustedCallers 属性并且是强命名的。

网站在完全信任的情况下运行。但是,将信任设置为中等后,我一浏览到该站点就会收到System.Security.SecurityException: Request failed. 错误。

在我的项目中,我调用了LogOnUser,以及多次调用各种System.DirectoryServices.AccountManagement 方法。

此站点能否以中等信任度运行,还是我必须完全信任所有 LDAP 调用?

正如我所提到的,我已经在所有项目上设置了AllowPartiallyTrustedCallers 属性。不知道还能做什么。
另外,我不知道错误是在什么/哪里产生的。服务器上的事件日志与此 SecurityException 无关。有什么方法可以找出错误位置,所以也许我可以尝试重写一些代码?

[在 Win2k8R2 上运行 .NET 4.0]

【问题讨论】:

  • 投反对票的任何理由???
  • 什么是LogOnUser?您的 AllowPartiallyTrustedCallers 程序集是在您的 bin 目录中还是在 GAC 中?
  • LogOnUser 是位于 advapi32.dll 中的 Win32 函数。它用于调用 Active Directory。我的问题是关于能够在中等信任下调用 Win32 dll 函数。另外,我的程序集不在 GAC 中,只是 bin。

标签: asp.net interop directoryservices medium-trust


【解决方案1】:

LogOnUser 与所有 P/Invoke 调用一样,需要带有 UnmanagedCode 权限标志的 SecurityPermissionSystem.DirectoryServices.AccountManagement 需要不受限制的 DirectoryServicesPermission。默认情况下,这两种权限都不会授予中等信任的 ASP.NET 应用程序。

AllowPartiallyTrustedCallers 属性允许部分信任程序集使用完全信任程序集。在您的情况下,该属性无效,因为 bin 文件夹中的所有程序集都加载到部分信任应用程序域中。

如果您的应用程序需要在中等信任下运行,并且您有能力将程序集安装到 GAC 中,那么您可以创建一个包含需要额外权限的代码的程序集,用 AllowPartiallyTrustedCallers 标记该程序集,然后将它在 GAC 中。您还需要Assert 所需的权限来禁止仍会发生的堆栈遍历。

有关详细信息,请参阅 MSDN 库中的 Code Access Security in ASP.NET 4 Applications 主题。

【讨论】:

    【解决方案2】:

    虽然@Michael Liu 的回答是正确的,但对于在这方面工作知识有限的新手(例如我自己)来说并不容易理解。此答案旨在补充迈克尔的答案。

    当您的 web.config 配置为使用任何除了“完整”的信任级别

    <trust level="Full"/>
    

    如果没有creating your own policy file,您将无法连接到 Active Directory。一旦您创建了自己的策略文件(假设它被称为“myPolicyFile.config”),那么您就可以对其进行自定义以允许您的 ASP.NET 应用程序连接到 Active Directory。

    以下是您需要进行的更改:

    在 web.config 中,配置网站以使用您的自定义策略文件:

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

    接下来,在您的 myPolicyFile.config 文件中,添加“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>
    

    注意: 节点可能已经存在(取决于您基于 myPolicyFile.config 文件的系统定义的信任级别文件)。如果它已经存在,那么您只需将文本“, UnmanagedCode”添加到“Flags”属性。如果它尚不存在,则将上面的示例复制并粘贴到“ASP.Net”NamedPermissionSet 中。

    最后,保存您的更改并重新加载您的网站。你应该很高兴去!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2011-02-01
      • 2010-11-22
      相关资源
      最近更新 更多