“通过绑定”试图模仿WCF中的Windows用户,不提供代表调用者的Windows标识

时间:2012-01-17 10:35:45

标签: .net wcf iis impersonation

我有一个用以下内容装饰的WCF服务:

[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]

我想在服务调用期间用于模拟的用户的Web.Config凭据中进行配置。用户是Windows域用户(凭据是:域\用户名和密码) 这是我的配置:

<behaviors>
  <serviceBehaviors>
    <behavior name="MetadataBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceAuthorization impersonateCallerForAllOperations="true" /> 
    </behavior>
  </serviceBehaviors>
</behaviors>
 <bindings>
  <basicHttpBinding>
    <binding name="httpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="MetadataBehavior" name="<serviceName>">
    <endpoint address="/" binding="basicHttpBinding" contract="<service contract>" bindingConfiguration="httpBinding"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="<service url>"/>
      </baseAddresses>
    </host>
  </service>
</services>

我收到错误:

合同操作'方法名称'需要Windows标识才能进行自动模拟。对于合同'合同名称','http://tempuri.org/',绑定('BasicHttpBinding','http://tempuri.org/')不提供代表调用者的Windows标识。

这是预期的,因为未在任何地方指定用户凭据。问题是:我应该在哪里放置凭据? 将它们放在system.web / identity中不起作用。我认为WCF需要单独配置它们。在哪里?

2 个答案:

答案 0 :(得分:1)

另一种获取当前调用者的Windows身份的方法如下:

var identity = ServiceSecurityContext.Current.WindowsIdentity;

此外,根据this MSDN page,您的绑定需要BasicHttpSecurityMode设置为TransportWithMessageCredential

答案 1 :(得分:0)

最终似乎只有在启用ASPNET兼容性的情况下才能执行冒充WCF方法:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

启用兼容模式后,来自system.web / identity的标准模拟起作用:

  <system.web>
        <authorization>
            <allow users="?"/>
        </authorization>
        <authentication mode="Windows"/>
        <identity impersonate="true" userName="mydomain\myuser" password="mypass"/>
  </system.web>

此解决方案不适用于更复杂的绑定,并且需要使用

修饰WCF实现类
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

所以它不适合所有情况,但对我来说这是可以接受的。