WCF - BasicHttpBinding, No user name and/or password is available, name: null, password: null
vb.net, wcf, web-services
Solution
After a lot of issues, I found that for some reason the Authorization header was not being sent to the service. In order to include it I had to do the below for the call to the service. What I am still confused about is why this is an issue. From what I thought BasicHttpBinding was supposed to include the credentials in the header by default? Any insight by someone as to why this was an issue for me would be greatly appreciated. And a good answer to that may still get the +50.
Private Function GetPdfListById(ByVal Id As Integer, ByVal Section As SectionId) As List(Of VendorGuideService.vogBlobName)
Using service As Service.ServiceClient = GetVendorService()
Dim nameList As New List(Of Service.blobName)
Using scope As ServiceModel.OperationContextScope = New ServiceModel.OperationContextScope(service.InnerChannel)
Dim request As New ServiceModel.Channels.HttpRequestMessageProperty()
request.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(service.ClientCredentials.UserName.UserName & ":" & _
service.ClientCredentials.UserName.Password)))
ServiceModel.OperationContext.Current.OutgoingMessageProperties(ServiceModel.Channels.HttpRequestMessageProperty.Name) = request
nameList.AddRange(service.getBlobNameByIdAndSectionId(section, id))
End Using
Return nameList
End Using
End Function
Problem
I'm trying to use a third party web service which is secured with a user/pass. I believe I have done what is needed to authenticate and set the user and pass, but it seems to not be including them in the http header or something... When attempting to call; ``` nameList.AddRange(service.getBlobNameByIdAndSectionId(section, id)) ``` I get this error; ``` No user name and/or password is available, name: null, password: null ``` Full code: ``` Private Function GetVendorService() As Services.ServiceClient Dim binding As New BasicHttpBinding(BasicHttpSecurityMode.Transport) binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic Dim ea As New EndpointAddress(GetVendorServiceURL()) Dim service As New Services.ServiceClient(binding, ea) service.ClientCredentials.UserName.UserName = "user" service.ClientCredentials.UserName.Password = "password" Return service End Function Public Function GetVendorServiceURL() As String Select Case Informix.HostType Case HostServerType.Stage Return "https://url-s.net:8443/cxf/Service/v1/ws" Case HostServerType.Dev Return "https://url-d.net:8443/cxf/Service/v1/ws" Case Else 'Live Return "https://url.net:8443/cxf/Service/v1/ws" End Select End Function Private Function GetPdfListById(ByVal Id As Integer, ByVal Section As SectionId) As List(Of Services.blobName) Dim service As Services.ServiceClient = GetVendorService() Dim nameList As New List(Of Services.blobName) service.Open() nameList.AddRange(service.getBlobNameByIdAndSectionId(section, id)) service.Close() Return nameList End Function ``` app.config ``` <system.serviceModel> <bindings> <basicHttpBinding> <binding name="ServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="Transport"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="https://url.net:8443/cxf/Service/v1/ws" binding="basicHttpBinding" bindingConfiguration="ServiceSoapBinding" contract="Services.Service" name="ServiceSoapPort" /> </client> ```