繁体   English   中英

像在WCF服务的ASP.NET中一样

[英]Impersionation like in ASP.NET for WCF Service

我在同一个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部分的Cookieless Session状态),我必须设置

aspNetCompatibilityEnabled="true" 

选项为false。 这样,我就失去了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>

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

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

我缺少重要的东西吗?

编辑:错误消息的翻译:

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

(原始错误消息是德语...)

尝试添加类似的东西

<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)> _

为我们工作

好吧,最后,我只使用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,虽然这不是正确的答案,但至少使我走上了正确的道路!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM