简体   繁体   English

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

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

I've got a WebService with ASP.NET sites and WCF services in the same web.config. 我在同一个web.config中有一个带有ASP.NET站点和WCF服务的WebService。 Until now, I was able to use the ASP.NET impersionation in the WCF services by setting 到目前为止,我可以通过设置WCF服务中的ASP.NET命令

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

However, now (for other reasons-> Cookieless Session state for the ASP.NET part) I have to set the 但是,现在(出于其他原因-> ASP.NET部分的Cookieless Session状态),我必须设置

aspNetCompatibilityEnabled="true" 

option to false. 选项为false。 With this I loose the ASP.NET impersionation for the WCF services. 这样,我就失去了WCF服务的ASP.NET功能。 One of my WCF services needs impersionation for IO operations on the server... I would like to know how to get the same impersionation I had before by directly defining it on the WCF service configuration. 我的WCF服务之一需要在服务器上进行IO操作的命令...我想知道如何通过直接在WCF服务配置上定义它来获得与以前相同的命令。

What I have tried (unsucessfully) is to set 我尝试过(失败)的是设置

[OperationBehavior(Impersonation = ImpersonationOption.Required)]

on the implementation of the methods in the WCF service and then specifying WCF服务中方法的实现,然后指定

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

in the web.config (obviously with the correct values for my service), as described in http://msdn.microsoft.com/en-us/library/ff650591.aspx . http://msdn.microsoft.com/en-us/library/ff650591.aspx中所述,在web.config中(显然具有我的服务的正确值)。

However, the WCF service can not be called anymore after this... It tells me that the WsHttpBinding does not offer an identity for the contract. 但是,此后将无法再调用WCF服务...它告诉我WsHttpBinding不提供合同的身份。

Am I missing something important? 我缺少重要的东西吗?

Edit: Translation of the error message: 编辑:错误消息的翻译:

: The contract operation '{0}' requires Windows identity for automatic impersonation. :合同操作'{0}'需要Windows身份才能自动模拟。 A Windows identity that represents the caller is not provided by binding ('{1}','{2}') for contract ('{3}','{4}'. 合同('{3}','{4}')的绑定('{1}','{2}')不能提供代表调用者的Windows身份。

(The original error message was german...) (原始错误消息是德语...)

Try adding someting similar to this 尝试添加类似的东西

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

This is the server side code 这是服务器端代码

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

As well as having these over our WebMethods 以及在我们的WebMethods中拥有这些

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

Works for us 为我们工作

Well, in the end I just made the binding use Windows authentication: 好吧,最后,我只使用Windows身份验证进行了绑定:

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

and passed a specific Windows user/pwd combination in the client: 并在客户端中传递了特定的Windows用户/密码组合:

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

Additionally I had to specifically use the newly impersonated user in the code of the web service: 另外,我必须在Web服务的代码中专门使用新模拟的用户:

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

Well, the actual (underlying) question still remains: 好吧,实际(基础)问题仍然存在:

How is it possible to emulate the ASP.NET functionality correctly... 如何正确模拟ASP.NET功能...

For the moment I'm ok with the solution, however I've got the feeling that I've missed an important point about the ASP.NET impersonation. 目前,我对解决方案还可以,但是我有一种感觉,就是我错过了有关ASP.NET模拟的重要要点。

Thanks a lot to Iain, although it wasn't exactly the correct answer, it at least got me on the right track! 非常感谢Iain,虽然这不是正确的答案,但至少使我走上了正确的道路!

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

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