2

我在同一个 web.config 中有一个带有 ASP.NET 站点和 WCF 服务的 WebService。到目前为止,我可以通过设置在 WCF 服务中使用 ASP.NET 模拟

<system.web>
    <compilation targetFramework="4.0" debug="false"/>
    <!-- switch custom errors of-->
    <identity impersonate="true"/>
    <customErrors mode="Off"/>
</system.web>

但是,现在(出于其他原因-> ASP.NET 部分的无 Cookie 会话状态)我必须设置

aspNetCompatibilityEnabled="true" 

选项为假。有了这个,我失去了对 WCF 服务的 ASP.NET 模拟。我的 WCF 服务之一需要对服务器上的 IO 操作进行模拟...我想知道如何通过直接在 WCF 服务配置上定义它来获得与以前相同的模拟。

我尝试过(不成功)是设置

[OperationBehavior(Impersonation = ImpersonationOption.Required)]

关于 WCF 服务中方法的实现,然后指定

<endpoint address="" binding="wsHttpBinding" contract="IService">
  <identity>
    <servicePrincipalName value="HOST/YourMachineName" />
    <dns value="" />
  </identity>
</endpoint>

在 web.config 中(显然我的服务具有正确的值),如http://msdn.microsoft.com/en-us/library/ff650591.aspx中所述。

但是,在此之后无法再调用 WCF 服务......它告诉我 WsHttpBinding 不为合同提供身份。

我错过了什么重要的东西吗?

编辑:错误信息的翻译:

: 合同操作“{0}”需要 Windows 身份才能自动模拟。代表调用者的 Windows 身份不是通过为合同 ('{3}'、'{4}' 的绑定 ('{1}'、'{2}') 提供的。

(原来的错误信息是德语...)

4

2 回答 2

1

尝试添加与此类似的内容

<system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="DelegationBehaviour">
                    <clientCredentials>
                        <windows allowNtlm="false" allowedImpersonationLevel="Delegation"></windows>
                    </clientCredentials>
                    <dataContractSerializer maxItemsInObjectGraph="4194304"></dataContractSerializer>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_SampleWebService" >
                    <readerQuotas maxArrayLength="16384" maxBytesPerRead="4096" maxDepth="32" maxNameTableCharCount="16384" maxStringContentLength="8192"></readerQuotas>
                    <security mode="TransportCredentialOnly">
                        <message algorithmSuite="Default" clientCredentialType="UserName"></message>
                        <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""></transport>
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://server/WebServices/Service/Service.svc" behaviorConfiguration="DelegationBehaviour" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SampleWebService" contract="SampleWS" name="BasicHttpBinding_SampleEndpoint"></endpoint>
        </client>
    </system.serviceModel>

这是服务器端代码

 <system.serviceModel>
    <services>
      <service behaviorConfiguration="CustomBehavior" name="CustomWebService">
        <endpoint address="" behaviorConfiguration="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_Service" contract="WebService"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding_Service" maxReceivedMessageSize="4194304" receiveTimeout="00:30:00">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows"/>
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomBehavior">
          <dataContractSerializer maxItemsInObjectGraph="4194304" ignoreExtensionDataObject="True"/>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
          <serviceAuthorization impersonateCallerForAllOperations="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

以及通过我们的 WebMethods 获得这些

<WebMethod(), OperationContract(), OperationBehavior(Impersonation:=ImpersonationOption.Required)> _

为我们工作

于 2012-10-08T09:18:33.523 回答
0

好吧,最后我只是让绑定使用 Windows 身份验证:

 <security mode="TransportWithMessageCredential">
        <message  negotiateServiceCredential="false" clientCredentialType="Windows" algorithmSuite="Default"/>
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
 </security>

并在客户端传递了一个特定的 Windows 用户/密码组合:

channelFactory.Credentials.Windows.ClientCredential = new NetworkCredential(@"", "", "");
channelFactory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

此外,我必须在 Web 服务的代码中专门使用新模拟的用户:

 using (var imp = ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
 {
     // do IO here
 }

好吧,实际(潜在)问题仍然存在:

如何正确模拟 ASP.NET 功能...

目前我对解决方案没意见,但是我感觉我错过了关于 ASP.NET 模拟的重要一点。

非常感谢 Iain,虽然它不是完全正确的答案,但它至少让我走上了正确的轨道!

于 2012-10-08T12:31:34.140 回答