【问题标题】:WCF Service enabling or disabling Security by changing configurationWCF 服务通过更改配置启用或禁用安全性
【发布时间】:2014-10-14 09:22:51
【问题描述】:

我有一个 WCF 服务应用程序,我只想通过更改配置来启用或禁用安全性。

我创建了两个basicHttpBinding 元素,一个具有安全性,一个没有安全性。根据我应用的绑定,我启用或禁用服务的安全性。

这是我的配置:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="HostedBasicHttpBinding">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
    <binding name="SecuredBasicHttpBinding">
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="SecuredService.CustomerService" behaviorConfiguration="DefaultBehavior">
    <endpoint address="" binding="basicHttpBinding" contract="SecuredService.ICustomerService" bindingConfiguration="HostedBasicHttpBinding"/>
    <endpoint address="mex"  binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
    <behavior name="SecuredServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="SecuredService.CustomerClientValidator, SecuredService"/>
      </serviceCredentials>
      <serviceAuthorization serviceAuthorizationManagerType="SecuredService.CrmServiceAuthorizationManager, SecuredService"  principalPermissionMode="Custom">
        <authorizationPolicies>
          <add policyType="SecuredService.AuthorizationPolicy, SecuredService"/>
        </authorizationPolicies>
      </serviceAuthorization>
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
    <add binding="basicHttpBinding" scheme="http" bindingConfiguration="HostedBasicHttpBinding" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

在这里,当我应用绑定HostedBasicHttpBinding 和行为DefaultBehavior 时,我希望我的服务没有启用任何身份验证或授权。当我应用 SecuredBasicHttpBindingSecuredServiceBehavior 时,我希望我的服务应用所有配置的安全性(身份验证和授权)。

现在的问题是,即使我申请了HostedBasicHttpBindingDefaultBehavior,授权似乎也会启动并引发诸如“访问被拒绝”之类的错误。

这里是PrinciplePermission的服务代码:

    [PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
    public void UploadEmployees(CustomerRequest request)
    {

        ProcessEmployees(request.PacketId, request.Employees);         

    }

当我在没有安全性的情况下应用配置绑定时,如何禁用授权。我的要求是仅通过配置启用或禁用,而不是触摸代码。

【问题讨论】:

    标签: c# web-services wcf security


    【解决方案1】:

    我看到一个选项 - 检查权限 programmatically(禁用安全性时跳过)

    为了保持您的代码干净,您可以将检查代码移动到单一方法中,并在您需要的任何地方使用它。甚至使用静态 IL 编织(编译时 AOP)——例如 Fody(方法装饰器编织器)

    【讨论】:

    • 我不想使用程序检查,因为它会污染服务代码。我想保持服务代码干净并仅通过属性应用安全性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2012-02-13
    相关资源
    最近更新 更多