自托管 WCF 服务和 basicHttpBinding:绑定不提供表示调用者的 Windows 标识

Posted

技术标签:

【中文标题】自托管 WCF 服务和 basicHttpBinding:绑定不提供表示调用者的 Windows 标识【英文标题】:Selfhosting WCF Service and basicHttpBinding: A Windows identity that represents the caller is not provided by binding 【发布时间】:2013-11-01 18:15:58 【问题描述】:

我在控制台应用程序中有一个自托管 wcf 服务。 一个简单的自托管服务没问题,例子够多了。

现在我想模拟 wcf 服务的调用者。虽然我关注了这篇 msdn 文章 http://msdn.microsoft.com/en-us/library/ff648505.aspx 我得到了以下错误:

合同操作“GetProduct”需要 Windows 身份才能自动模拟。绑定不提供代表调用者的 Windows 标识

这是我在 CONSOLE 应用程序中 wcf 服务的 App.config:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="SelfHostingWCFService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <SelfHostingWCFService.Properties.Settings>
      <setting name="URL" serializeAs="String">
        <value>http://localhost:8081/ProductService</value>
      </setting>
    </SelfHostingWCFService.Properties.Settings>
  </applicationSettings>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ProductServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceAuthorization impersonateCallerForAllOperations="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="SelfHostingWCFService.ProductService"
                behaviorConfiguration="ProductServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081/ProductService"/>
          </baseAddresses>
        </host>
        <endpoint address="soap" binding="basicHttpBinding" contract="SelfHostingWCFService.IProductService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>
</configuration>

这里是服务代码:

    public class ProductService : IProductService

     [OperationBehavior(Impersonation = ImpersonationOption.Required)]
    public Product GetProduct(int productId)
    
        Product prod = new Product();
        prod.ID = productId;
        prod.Name = productId.ToString() + " XDS";

        return prod;
    

     [OperationBehavior(Impersonation = ImpersonationOption.Required)]
    public string User()
    
        string Name = string.Empty; 

        if (OperationContext.Current.ServiceSecurityContext != null)
            Name = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;

        if (ServiceSecurityContext.Current != null)
            Name = ";" + ServiceSecurityContext.Current.WindowsIdentity.Name;

        Name = ";" + Thread.CurrentPrincipal.Identity.Name;

        return Name;
    

这是客户端代码:

    private void button1_Click(object sender, EventArgs e)
    
        ProductService.ProductService srv = new ProductService.ProductService();

        //srv.Credentials = System.Net.CredentialCache.DefaultCredentials;
        ProductService.Product prod = srv.GetProduct(1, true);
        label1.Text = prod.Name;
        label2.Text = srv.User();

    

我做错了什么? 请告诉我。

我可以使用 basicHTTPBinding 还是必须使用 wsHTTPBinding?

非常感谢您的帮助

【问题讨论】:

【参考方案1】:

我没有看到您的客户端配置 - 您可能需要设置您的客户端绑定 Security.Transport.ClientCredentialType

请看这里:http://msdn.microsoft.com/en-us/library/ms729700(v=vs.110).aspx

【讨论】:

如果我将我的 WCF 服务添加为服务参考而不是 Web 参考(如上所述),app.config 会自动生成并且调用有效!非常感谢。在您的帮助下,我找到了间接的解决方案。

以上是关于自托管 WCF 服务和 basicHttpBinding:绑定不提供表示调用者的 Windows 标识的主要内容,如果未能解决你的问题,请参考以下文章

具有 basicHttpBinding、传输安全性和基本客户端凭据类型的自定义 WCF 凭据验证器

如何从服务“内部”的代码访问托管在 IIS 中的 WCF 服务的 web.config

使用 BasicHttpBinding 进行身份验证的 WCF 服务

WCF 服务的 basicHttpBinding 上的 HTTPS

WCF 4 Web 服务中的 UsernameToken 和 SSL - 但使用 basicHttpBinding

WCF 服务 maxReceivedMessageSize basicHttpBinding 问题