代码之家  ›  专栏  ›  技术社区  ›  Michael Johnson

在WCF和Castle Windsor中使用IAuthorizationPolicy时出现问题

  •  1
  • Michael Johnson  · 技术社区  · 15 年前

    IAuthorizationPolicy ,这在基于服务的每个方法完成时似乎有效,但是当服务类本身被标记为需求时,我会引发异常。

    How To – Use Username Authentication with Transport Security in WCF from Windows Forms Membership 实施。我的 授权策略 实施( HttpContextPrincipalPolicy )本质上是相同的。

    我的基本部分 Web.config

    <serviceBehaviors\>
      <behavior name="MyBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
        <serviceAuthorization principalPermissionMode="UseAspNetRoles"
                              roleProviderName="UserProvider">
          <authorizationPolicies>
            <clear/>
            <add policyType="Website.FormsAuth.HttpContextPrincipalPolicy,Website"/>
          </authorizationPolicies>
        </serviceAuthorization>
      </behavior>
    </serviceBehaviors>
    

    [PrincipalPermission(SecurityAction.Demand, Role = RoleNames.USER_ADMINISTRATION)]
    

    如果这是在 OperationContract 方法,一切按预期进行。但是,如果它被移动到类本身(它实现 ServiceContract )我得到了以下异常(大多数额外的东西都被删掉了):

    Castle.MicroKernel.ComponentActivator.ComponentActivatorException {
        Message = "ComponentActivator: could not instantiate Services.UserService"
        InnerException = System.Reflection.TargetInvocationException {
            Message = "Exception has been thrown by the target of an invocation."
            InnerException = System.Security.SecurityException {
                Message = "Request for principal permission failed."
            }
        }
    }
    

    HttpContextPrincipalPolicy公司 正在被呼叫,但是 Evaluate() 不是当需求附加到类时。当它附加到方法时 被召唤。所以在这一点上,我已经尽我的新手.NET/WCF/Castle Windsor技能所能。

    有没有办法告诉Castle Windsor在执行 计算()

    1 回复  |  直到 15 年前
        1
  •  1
  •   Drew Marsh    15 年前

    PrincipalPermissionAttribute 这实际上是在对运行时说,在使用类时,必须满足权限需求。所以现在,当Castle Windsor试图实例化这个类时,权限需求正在生成,当然它无法满足,因为安全上下文在那时没有正确建立。

    阿法克, 由于WCF运行时的性质,它在类级别上不受支持,即使它是从纯.NET角度允许的。因此,Castle Windsor无法基于相同的原因创建您的服务实例。