WCF服务身份验证失败

本文关键字:失败 身份验证 服务 WCF | 更新日期: 2023-09-27 18:09:27

我正在尝试在我的控制台应用程序中使用WCF服务。我的App.Config文件看起来像这样

<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="asd@as" />
            </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.

有谁能指出我错过了什么吗?

WCF服务身份验证失败

1打开客户端项目属性。

。转到services选项卡

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

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