【问题标题】:"A Windows identity that represents the caller is not provided by binding" trying to impersonate windows user in WCF“绑定不提供代表调用者的 Windows 标识”试图在 WCF 中模拟 Windows 用户
【发布时间】:2012-02-12 04:02:41
【问题描述】:

我有一个 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 身份才能自动模拟。代表调用者的 Windows 身份不是由合约“合约名称”、“http://tempuri.org/”的绑定(“BasicHttpBinding”、“http://tempuri.org/”)提供。

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

【问题讨论】:

    标签: .net wcf iis impersonation


    【解决方案1】:

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

    var identity = ServiceSecurityContext.Current.WindowsIdentity;
    

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

    【讨论】:

    • 添加 BasicHttpSecurityMode 没有帮助。我需要冒充来电者。呼叫者根本不应该在通话期间进行授权。我想在 web.config 中配置应该使用哪些凭据。我想实现这一点:调用者将在未经授权的情况下调用该方法,代码将以配置用户的身份执行。
    • 我不确定 WCF 中有没有内置的方法可以做到这一点。我认为您必须从 web.config 获取身份,并在需要模拟的任何地方使用 ImpersonationScope。据我所知,WCF 中的内置模拟总是尝试使用调用服务的用户的身份。
    • 我看到你的配置中有这个: 这不是你想要的,因为你说过你想忽略调用者并使用预先在您的 web.config 中配置身份
    • 您对 的权利。这不是我需要的。我需要一种方法来模拟 web.config 中的预配置身份
    【解决方案2】:

    最终似乎只有在启用 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)]
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多