问题描述
我有一个.net核心2.1 Web API,它使用Windows身份验证调用WCF服务:
appsettings.json
"MyService": {
"BasicHttpBinding": {
"Security": {
"Mode": "TransportCredentialOnly","Transport": {
"ClientCredentialType": "Windows"
}
}
},"UserPrincipalName": "mydomain/myUser"
},
mydomain / myUser-它是有权调用MyService和MyMethod()的用户
code.cs
using (var client = new ServiceClient<IMyService>(BasicHttpBinding,new EndpointAddress(new Uri(myServiceUrl),UpnEndpointIdentity)))
{
var result = client.Proxy.MyMethod();
return result;
}
HTTP请求未经客户端身份验证方案授权 '谈判'。从服务器收到的身份验证标头是“协商”
我试图在startup.cs中指示身份验证模式:
services.AddAuthentication(IISDefaults.Negotiate);
但没有运气,无论如何还是会出现异常。
如何解决此问题?
解决方法
如果使用Windows身份验证,则客户端在调用服务器时需要提供Windows凭据:
Service1Client service1Client = new Service1Client();
service1Client.ClientCredentials.Windows.ClientCredential.UserName = "Administrator";
service1Client.ClientCredentials.Windows.ClientCredential.Password = "Password";
如果客户端不提供Windows凭据,则会报告错误:
如果问题仍然存在,请随时告诉我。
更新
尝试使用 WCF Web服务参考添加服务参考以调用该服务:
ServiceReference1.Service1Client service1Client = new ServiceReference1.Service1Client();
我将为您介绍我的项目,该项目已部署在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>
这是web.config。
在IIS中,我为WCF添加了两个绑定,一个是http,另一个是https,并且必须使用证书才能使用https:
我们还需要关闭匿名身份验证并启用Windows身份验证:
如果将模式设置为传输,我们还需要添加htts绑定和证书。
您可以尝试上述步骤来重新部署,希望您能成功。