Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF - BasicHttpBinding, No user name and/or password is available, name: null, password: null

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>

like image 762
Tomcat Avatar asked Apr 30 '13 15:04

Tomcat


People also ask

How do I bypass WCF username and password?

To configure a service to authenticate its clients using Windows Domain username and passwords use the WSHttpBinding and set its Security. Mode property to Message . In addition you must specify an X509 certificate that will be used to encrypt the username and password as they are sent from the client to the service.

What is BasicHttpBinding?

Basic binding is offered by the BasicHttpBinding class. It uses the HTTP protocol to transport and represent a WCF service as an ASP.NET web service (ASMX web service), so that old clients who use ASMX web services can consume the new services conveniently.


1 Answers

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
like image 54
Tomcat Avatar answered Sep 20 '22 06:09

Tomcat