使用带有net.tcp绑定的WCF模拟访问web.config设置

时间:2010-01-11 00:20:06

标签: .net wcf wcf-binding wcf-security impersonation

我正在尝试使用我在IIS 7中托管的WCF服务(使用net.tcp绑定)进行模拟。我已经到了冒充客户端但我尝试访问网络中的任何配置设置时.config使用Settings.Default.SomeSetting它会抛出一个SettingsPropertyNotFoundException。 这是因为IIS在与模拟身份不同的身份下运行吗?如果是这样,我必须更改哪些设置以允许它们在相同的模拟身份下运行? 我尝试过设置“servicePrincipalName”属性没有任何成功。

我在下面添加了我的web.config设置:

<system.serviceModel>
    <services>
      <service name="TestServices">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding"
          contract="Test.ITestService">
          <identity>
            <servicePrincipalName value="NT AUTHORITY\NETWORK SERVICE" />
          </identity>
        </endpoint>
        <endpoint address="mextcp" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
    </services>        
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <bindings>          
      <netTcpBinding>
        <binding name="tcpbinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" portSharingEnabled="true">
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="None"/>
            <message clientCredentialType="Windows"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization impersonateCallerForAllOperations="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:2)

似乎我没有在服务器端正确模仿我的客户端,因为我需要将客户端上的allowedImpersonationLevel设置为“Impersonation”。默认为“标识”。因此,当我使用WindowsIdentity.GetCurrent()测试时,名称我得到了正确的用户名,但用户实际上并没有被模仿。

因此,将此添加到我的客户端web.config就可以了:

    <client>
      <endpoint address="net.tcp://localhost/Test/Service/TestService.svc"
          binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITestService"
          contract="ServiceReference.ITestService" name="NetTcpBinding_ITestService"
          behaviorConfiguration="ImpersonationBehavior">
      </endpoint>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ImpersonationBehavior">
          <clientCredentials>
            <windows allowedImpersonationLevel="Impersonation" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>