【问题标题】:Impersionation like in ASP.NET for WCF ServiceWCF 服务的 ASP.NET 中的模拟
【发布时间】:2012-10-08 07:56:39
【问题描述】:

我在同一个 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}') 提供的。

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

【问题讨论】:

  • “不提供合同身份” - 请显示确切的错误消息。

标签: c# asp.net wcf


【解决方案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)> _

为我们工作

【讨论】:

  • 感谢您的回答。我检查一下是否可以让这样的事情起作用。
  • 还没有开始工作。我还必须让客户端使用服务配置...
  • 实际上,我正在尝试正确设置服务器配置文件...这似乎是客户端配置文件(但是,我正在尝试描述的安全设置)。
【解决方案2】:

好吧,最后我只是让绑定使用了 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,虽然它不是完全正确的答案,但它至少让我走上了正轨!

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多