none
BasicHttpBinding username and password with Windows Phone 8 RRS feed

  • Question

  • I am trying to consume a 3rd party web service. They advise they have security mode set to Transport Credential Only and client Credential Type is set to Basic. In IIS they ONLY have Basic Authentication enabled.

    I can access the service over a web browser using what i assume could be local accounts on the server.

    I write some code to access the service using WP8:

    ThirdPartService = New SomeService.SomeServiceClient
    ThirdPartService.ClientCredentials.UserName.UserName = "user"
    ThirdPartService.ClientCredentials.UserName.Password = "password"

    But constantly receive the error "The remote server returned an unexpected response: (401) Unauthorized. In Silverlight, a 404 response code may be reported even when the service sends a different error code.

    "

    What am i doing wrong? I have posted this at the Windows Phone forum but get the feeling no one knows the answer to this question there.

    Tuesday, December 17, 2013 7:10 PM

All replies

  • Hi,

    First please try to refer to this article about how to achive it:
    #How to implement Basic HTTP Authentication in WCF on Windows Phone:
    http://cisforcoder.wordpress.com/2010/12/01/how-to-implement-basic-http-authentication-in-wcf-on-windows-phone-7/ .

    Then please try to enable the wcf tracing to find the cause of this exception.

    The following configuration taken from MSDN can be applied to enable tracing on your WCF service.

    <configuration>
      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true" >
            <listeners>
                 <add name="xml"/>
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
                <add name="xml"/>
            </listeners>
          </source>
          <source name="myUserTraceSource"
                  switchValue="Information, ActivityTracing">
            <listeners>
                <add name="xml"/>
            </listeners>
          </source>
        </sources>
        <sharedListeners>
            <add name="xml"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="Error.svclog" />
        </sharedListeners>
      </system.diagnostics>
    </configuration>

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 18, 2013 10:36 AM
    Moderator
  • At present i have the below code: However i've not added the tracelog just yet just to ensure i have the code correct. The problem i have now is the ConnectionState is always "Created"

        Private Function OpenTPServiceConnection() As System.ServiceModel.CommunicationState
            _myService = New TPService.TPClient

            Dim credentials = EncodeBasicAuthenticationCredentials("user", "somepassword")

            Using scope As New OperationContextScope(_myService.InnerChannel)
                Dim request As New HttpRequestMessageProperty()
                request.Headers(System.Net.HttpRequestHeader.Authorization) = "Basic " & Convert.ToString(credentials)

                OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, request)

                If _myService.State <> ServiceModel.CommunicationState.Opened Then
                    Try
                        _myService.OpenAsync()
                    Catch ex As Exception
                        MessageBox.Show(ex.Message)
                    End Try
                End If
            End Using

            Return _myService.State
        End Function

        Private Function EncodeBasicAuthenticationCredentials(username As String, password As String) As String
            Dim credentials As String = username & ":" & password

            Dim asciiCredentials = (From ch In credentials Select If(Char.GetNumericValue(ch) < 127, Convert.ToByte(ch), Convert.ToByte("?"c))).ToArray()

            Return Convert.ToBase64String(asciiCredentials)
        End Function

       Public Sub LoadData
       If OpenTPServiceConnection.OpenSync Then
       ' GetData
       End If
       End Sub

    Wednesday, December 18, 2013 9:24 PM
  • I added logging and viewing the service in IE prompts for a username and password. Upon entering those details all seems fine.

    I then add the service to my application and the connection state remains on "Created" but doesnt open even though it runs past that line of code.

    Looking at the log nothing is written (log found under the WCF service), i also searched for error.svclog but nothing was found in my project.

    Anything else i could try?

    Thursday, December 19, 2013 8:44 PM
  • Anyone?
    Friday, December 20, 2013 6:59 PM