2011-07-06 34 views
0

我試圖實現我的自定義安全屬性。這是現在自定義安全屬性中的無效mscorlib異常c'tor

[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服務中保留 我很樂意得到一些幫助。

我的服務配置如下

<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> 

我得到的錯誤都在Windows XP,IIS V5.1和Windows Server只有在WCF服務配置爲使用2008 R2 IISV7.5上Windows身份驗證(請參閱上面的配置)。更有趣的事實是,僅當該屬性與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); 
} 

其他選項正常工作。 謝謝。

+0

不幸的是,我似乎無法重現這個問題。該屬性是在您的服務項目中還是在單獨的DLL中聲明的?當你說DLL被簽名,哪個DLL被簽名以及它是什麼類型的簽名(強名稱或者真實代碼)?另外,您的代碼的哪些部分已經應用了該屬性? –

+0

嗨@Nicole,我試過了,單獨的dll和服務的dll,結果是一樣的。現在,爲了簡單起見,該屬性在與服務相同的dll中定義,並且使用字符串名稱(* .snk文件)對dll進行簽名。該屬性應用於Service方法(實現,而不是合同)。 –

+0

我仍然不能重現這個問題。你如何主辦這項服務?此外,您是否對默認行爲,激活等進行了任何更改? –

回答

0

在我的一位同事的幫助下,問題已經解決。我不確定例外的確切原因是什麼,但似乎是編譯問題。當我將項目類型從Web應用程序更改爲網站時,根據其定義的池(64或32位)在運行時編譯它,它開始正常工作。

相關問題