【问题标题】:invalid mscorlib exception in custom security attribute c'tor自定义安全属性 c'tor 中的无效 mscorlib 异常
【发布时间】:2011-07-06 12:01:16
【问题描述】:

我正在尝试实现我的自定义安全属性。现在很简单

    [Serializable]
    [ComVisible(true)]
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class SecPermissionAttribute : CodeAccessSecurityAttribute
    {
        public SecPermissionAttribute(SecurityAction action) : base(action) { }

        public override System.Security.IPermission CreatePermission()
        {
            IPermission perm = new PrincipalPermission(PermissionState.Unrestricted);
            return perm;
        }
    }

由于某种原因,我在属性 c'tor 中有一个异常

System.IO.FileLoadException occurred
  Message=The given assembly name or codebase, 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', was invalid.
  Source=WcfRoleProviderTestService
  StackTrace:
       at SecLib.SecPermissionAttribute..ctor(SecurityAction action)
       at WcfRoleProviderTestService.Service1.GetData(Int32 value) in D:\TestProjects\WcfRoleProviderTestService\WcfRoleProviderTestService\Service1.svc.cs:line 19
  InnerException: 

dll 已签名。在我看来,这像是一个安全问题,但我不确定。顺便说一句,我尝试使用 PrincipalPermissionAttribute 并且效果很好。 忘了说,我用的是VS 2010,FW 4.0,属性在WCF服务中concumed 我会很高兴得到一些帮助。

我的服务配置如下

<system.web>
    <compilation debug="true" defaultLanguage="c#" targetFramework="4.0" />

    <roleManager enabled="true" cacheRolesInCookie="true" cookieName=".ASPROLES"
      defaultProvider="MyRoleProvider">
      <providers>
        <clear />
        <add connectionStringName="Service1" applicationName="InfraTest"
          writeExceptionsToEventLog="false" name="MyRoleProvider" type="SecLib.MyRoleProvider, SecLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=798c04e15cff851a" />
      </providers>
    </roleManager>

  </system.web>

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBindingConfiguration" closeTimeout="00:01:00"
          sendTimeout="00:10:00" maxBufferSize="524288" maxReceivedMessageSize="524288">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfRoleProviderTestService.Service1"
               behaviorConfiguration="BasicHttpServiceBehavior" >
        <endpoint name="BasicHttpEndpoint"
                  contract="WcfRoleProviderTestService.IService1"
                  address="WcfAuthenticationTest"
                  binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBindingConfiguration" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/WcfRoleProviderTestService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="BasicHttpServiceBehavior">
          <serviceAuthorization principalPermissionMode="UseAspNetRoles"
            roleProviderName="MyRoleProvider" impersonateCallerForAllOperations="true" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

仅当 WCF 服务配置为使用 Windows 身份验证时,我在 Windows XP、IIS v5.1 和 Windows Server 2008 R2 IISV7.5 上都遇到了错误(请参阅上面的配置)。更有趣的事实是,仅当属性与 System.Security.Permissions.SecurityAction.Demand 安全操作一起使用时才会发生错误。

[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
[SecPermission(System.Security.Permissions.SecurityAction.Demand)]
public string GetData(int value)
{
      string userName = ServiceSecurityContext.Current.WindowsIdentity.Name;
      return string.Format("You entered: {0}, User {1}", value, userName);
}

其他选项工作正常。 谢谢。

【问题讨论】:

  • 不幸的是,我似乎无法重现这个问题。该属性是在您的服务项目中还是在单独的 DLL 中声明的?当你说 DLL 被签名时,是哪个 DLL 被签名的,它是什么类型的签名(强名或验证码)?另外,您在代码的哪些部分应用了该属性?
  • 嗨@Nicole,我都试过了,一个单独的dll和服务dll,结果是一样的。目前,为了简单起见,属性定义在与服务相同的 dll 中,并且 dll 使用字符串名称(*.snk 文件)进行签名。该属性应用于服务方法(实现,而不是合同)。
  • 我仍然无法重现该问题。您如何托管服务?另外,您是否对默认行为、激活等进行了任何更改?
  • 嗨@Nicole,谢谢你帮助我。答案是肯定的,我已经进行了更改,我将 windows 身份验证与尚未使用的自定义角色提供程序一起使用。
  • 我已将配置添加到问题中。我试图评论提供者部分,结果是一样的。顺便说一句,PrincipalPermissionAttribute 工作正常,没有问题,不幸的是它的功能对我来说还不够

标签: wcf c#-4.0 windows-xp iis-5 windows-authentication


【解决方案1】:

在我的一位同事的帮助下,问题已得到解决。我不确定异常的确切原因是什么,但这似乎是一个编译问题。当我将项目类型从 Web 应用程序更改为在运行时根据其池定义(64 位或 32 位)编译的网站时,它开始正常工作。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2016-01-07
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多