自托管WCF服务和basicHttpBinding:绑定不提供代表调用者的Windows身份

时间:2013-11-01 18:15:58

标签: wcf console-application self-hosting

我在控制台应用程序中拥有自托管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 个答案:

答案 0 :(得分:0)

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

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