2014-06-20 230 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));