2014-06-20 232 views
0

我想在我的控制台应用程序中使用WCF服务。 我App.config文件看起来像这样WCF服务身份验证失败

<configuration> 
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
</startup> 
<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="WSHttpBinding_InventItemGroupService" /> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://mydomain.com/MicrosoftDynamicsAXAif50/inventitemgroupservice.svc" 
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_InventItemGroupService" 
      contract="ServiceReference1.InventItemGroupService" name="WSHttpBinding_InventItemGroupService"> 
      <identity> 
       <userPrincipalName value="[email protected]" /> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

控制台应用程序代码,以使身份验证的一部分。

protected ProgramClass(AifSos.InventItemGroupServiceClient inventItemGroupServiceClient) // Constructor 
    { 
     MInventItemGroupServiceClient = inventItemGroupServiceClient; 
     // ReSharper disable once PossibleNullReferenceException 
     MInventItemGroupServiceClient.ClientCredentials.Windows.ClientCredential.UserName = "un"; 
     MInventItemGroupServiceClient.ClientCredentials.Windows.ClientCredential.Password = "pw"; 
     MInventItemGroupServiceClient.ClientCredentials.Windows.ClientCredential.Domain = "domain"; 
    } 

一切似乎都没有问题啊,但它总是抛出一个错误

The caller was not authenticated by the service. 

任何一个能指出我错过了什么?

回答

1

1转到您的客户项目属性。

a。转到服务选项卡

启用此设置并使用身份验证模式窗口

2变化app.config文件的客户项目中与此两个样本线服务项目中

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

3变化app.config文件

<security mode="None"> 
      <message clientCredentialType="Windows" negotiateServiceCredential="false" algorithmSuite="Default" establishSecurityContext="false" /> 
      </security> 

4在客户端代码中创建服务实例并调用服务时使用此行提供服务器中的登录信息副PC。

  Service1Client client = new Service1Client(); 
      client.ClientCredentials.Windows.ClientCredential.UserName = "ETLIT-1"; 
      client.ClientCredentials.Windows.ClientCredential.Password = "etl"; 
      client.ClientCredentials.Windows.AllowNtlm = false; 
      client.ClientCredentials.Windows.ClientCredential.Domain = "ETLIT-1-PC"; 
      Console.WriteLine(client.addNumber(23, 2));