Selfhosting WCF Service and basicHttpBinding: A Windows identity that represents the caller is not provided by binding

StackOverflow https://stackoverflow.com/questions/19732821

Pregunta

I have a self hosting wcf service in a console application. A simple self hosting service is no problem, there are enough examples.

Now I want to impersonate the caller of the wcf service. Although I followed this msdn article http://msdn.microsoft.com/en-us/library/ff648505.aspx I get follwing error:

The contract operation 'GetProduct' requires Windows identity for automatic impersonation. A Windows identity that represents the caller is not provided by binding

Here is my App.config of the wcf service in the CONSOLE application:

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

Here ist the Service Code:

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

Here is the client code:

    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();

    }

What do I making wrong? Please let me know.

Can I use basicHTTPBinding or must I use wsHTTPBinding?

many thanks for your help

¿Fue útil?

Solución

I don't see your client-side configuration - you may need to set your client binding Security.Transport.ClientCredentialType

See here: http://msdn.microsoft.com/en-us/library/ms729700(v=vs.110).aspx

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top