Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net核心中调用wcf的身份验证方案_C#_Wcf_Authentication_Asp.net Core 2.1 - Fatal编程技术网

C# .net核心中调用wcf的身份验证方案

C# .net核心中调用wcf的身份验证方案,c#,wcf,authentication,asp.net-core-2.1,C#,Wcf,Authentication,Asp.net Core 2.1,我有一个.net core 2.1 web api,通过windows身份验证调用WCF服务: appsettings.json "MyService": { "BasicHttpBinding": { "Security": { "Mode": "TransportCredentialOnly", &

我有一个.net core 2.1 web api,通过windows身份验证调用WCF服务:

appsettings.json

"MyService": {
    "BasicHttpBinding": {
            "Security": {
                "Mode": "TransportCredentialOnly",
                "Transport": {
                    "ClientCredentialType": "Windows"
                }
            }
        },
        "UserPrincipalName": "mydomain/myUser"
},
mydomain/myUser-它是一个有权调用MyService和MyMethod()的用户

代码.cs

        using (var client = new ServiceClient<IMyService>(BasicHttpBinding, new EndpointAddress(new Uri(myServiceUrl), UpnEndpointIdentity)))
        {
            var result = client.Proxy.MyMethod();
            return result;
        }
但没有运气,例外情况无论如何都会重现


如何解决此问题?

如果使用windows身份验证,客户端在调用服务器时需要提供windows凭据:

 Service1Client service1Client = new Service1Client();
 service1Client.ClientCredentials.Windows.ClientCredential.UserName = "Administrator";
 service1Client.ClientCredentials.Windows.ClientCredential.Password = "Password";
如果客户端不提供windows凭据,将报告错误:

如果问题仍然存在,请随时通知我

更新

尝试使用WCF Web服务引用添加服务引用以调用服务:

我将为您介绍部署在IIS中的我的项目:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2"/>
  </system.web>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="serviceBehaviour">
                    <serviceMetadata httpGetEnabled="true"  httpsGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="HttpBinding_IService">
                    <readerQuotas maxDepth="32" maxStringContentLength="1000000" maxArrayLength="163840000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"/>
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <services>
            <service name="WcfService1.Service1" behaviorConfiguration="serviceBehaviour">
                <endpoint address="Service" binding="basicHttpBinding" bindingConfiguration="HttpBinding_IService" contract="WcfService1.IService1" />
            </service>
        </services>
    </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

我们还需要关闭匿名身份验证并启用windows身份验证:

如果模式设置为传输,我们还需要添加htts绑定和证书。


您可以尝试上述步骤进行重新部署,希望您能成功。

看起来合乎逻辑,但不幸的是没有帮助。问题仍然存在:client.ClientCredentials.Windows.ClientCredential.UserName=“myUser”
client.ClientCredentials.Windows.ClientCredential.Password=“myPassword”
client.ClientCredentials.Windows.ClientCredential.Domain=“myDomain”是因为您提供了错误的windows凭据吗?需要提供服务器端windows凭据。凭据有效,我将自己的凭据放入。不幸的是,我不能提供错误的屏幕截图,因为这个问题只在测试环境中重现(在本地它工作正常)。日志截图:您尝试使用WCF Web服务引用添加服务引用以调用该服务。
 ServiceReference1.Service1Client service1Client = new ServiceReference1.Service1Client();
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2"/>
  </system.web>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="serviceBehaviour">
                    <serviceMetadata httpGetEnabled="true"  httpsGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="HttpBinding_IService">
                    <readerQuotas maxDepth="32" maxStringContentLength="1000000" maxArrayLength="163840000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"/>
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <services>
            <service name="WcfService1.Service1" behaviorConfiguration="serviceBehaviour">
                <endpoint address="Service" binding="basicHttpBinding" bindingConfiguration="HttpBinding_IService" contract="WcfService1.IService1" />
            </service>
        </services>
    </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>