我一直在努力寻找问题所在。我在网上搜寻了答案,但没有解决方法。
我们正在通过自定义创建的.dll调用Web服务。如果我们从Visual Studio客户端中调用服务(测试在自定义代码上运行),则该服务成功。如果我们从SoapUI调用该服务,它将成功,但是一旦从使用自定义代码的应用程序中调用该服务,它就会失败,并显示以下错误:
HTTP请求未经客户端身份验证方案“协商”授权。从服务器收到的身份验证标头是“ Negotiate,Kerberos”。
以下是我们使用的代码:
Public Function Get_Company_Data(url As String, username As String, password As String, logFilePath As String) As Company_Service.Report_EntryType()
LogToFile(logFilePath, "Start Get_Company_Data")
LogToFile(logFilePath, String.Format("URL: {0} UN: {1} PWD: {2}", url, username, password))
Dim client As New Company_Service.ReportPortClient(_customBinding, New ServiceModel.EndpointAddress(url))
client.ClientCredentials.UserName.UserName = username
client.ClientCredentials.UserName.Password = password
Dim reportParams As New Company_Service.Execute_ReportType
Dim returnValue As Company_Service.Report_EntryType()
Try
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3
returnValue = client.Execute_Report(reportParams)
LogToFile(logFilePath, String.Format("Retured {0} records", returnValue.Count))
Return returnValue
Catch ex As Exception
LogToFile(logFilePath, String.Format("Exception: {0}", ex))
Return Nothing
End Try
End Function
这是我们的绑定代码:
Private Function ConfigureCustomBinding() As ServiceModel.Channels.CustomBinding
Dim sb As ServiceModel.Channels.TransportSecurityBindingElement = ServiceModel.Channels.SecurityBindingElement.CreateUserNameOverTransportBindingElement()
sb.IncludeTimestamp = False
Const lim As Integer = Int32.MaxValue
Dim timeout = TimeSpan.FromMinutes(10)
Dim eb As New ServiceModel.Channels.TextMessageEncodingBindingElement(ServiceModel.Channels.MessageVersion.Soap11, Text.Encoding.UTF8)
Dim tb As New ServiceModel.Channels.HttpsTransportBindingElement()
tb.MaxBufferPoolSize = lim
tb.MaxBufferPoolSize = lim
tb.MaxReceivedMessageSize = lim
tb.MaxBufferSize = lim
tb.Realm = String.Empty
tb.AuthenticationScheme = AuthenticationSchemes.Negotiate
Dim cb As New ServiceModel.Channels.CustomBinding(sb, eb, tb)
cb.SendTimeout = timeout
cb.ReceiveTimeout = timeout
Return cb
End Function
真的希望有人可以提供帮助吗?