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

3
我正在尝试在IIS 7中托管的WCF服务(使用net.tcp绑定)中使用模拟身份。我已经达到了它模拟客户端的效果,但是每当我尝试使用Settings.Default.SomeSetting访问web.config中的任何配置设置时,它就会抛出一个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个回答

3

看起来我在服务器端没有正确模拟我的客户端,因为我需要在客户端上设置allowedImpersonationLevel为"Impersonation"。它的默认值是"Identification"。所以当我使用WindowsIdentity.GetCurrent().Name进行测试时,我得到了正确的用户名,但用户实际上并没有被模拟。

所以将以下内容添加到我的客户端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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接