none
[UWP] How to switch basic auth password encoding to UTF-8 RRS feed

  • Question

  • Hi,

    we maintain a W10 UWP App based on NETCore.UniversalWindowsPlatform 6.2.8. This App connects to a WebService. The client code has been automatically generated by WDSL import into "Connected Services" (Studio 2017).

    For authentication we add

                    httpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
                    httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    which works as expected and produces a request containing for example

                  contentType=[text/xml; charset=utf-8]
                  header=authorization=Basic enp6OkZ13w==

    Our problem: the password is always encoded as "windows-1251" (regardless of contentType), whereas the server expects UTF-8.

    I couldn't find any way to change the password encoding or to manipulate the HTTP header before sending. The implementation of request composition is hidden in System.ServiceModel.ClientBase.

    Is there any official way or workaround to do this?

    Best regards,

    Holger



    • Edited by PhoenixHsb Thursday, August 8, 2019 10:52 AM
    Thursday, August 8, 2019 9:41 AM

All replies

  • Hi,

    The first thing is that we do recommend to use http APIs from the Windows.Web.Http Namespace in UWP apps.  Then you could try to following code to change the default encoding.

    // Set the header with a strong type.
                string username = "user";
                string password = "password";
                var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary (username + ":" + password, Windows.Security.Cryptography.BinaryStringEncoding.Utf16LE);
                string base64token = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);
                request.Headers.Authorization = new HttpCredentialsHeaderValue("Basic", base64token);
    The sample is a base64 encoding, so you could change it to UTF-8. 

    You could get more information here:https://docs.microsoft.com/en-us/uwp/api/windows.web.http.headers.httpcredentialsheadervalue

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 9, 2019 3:02 AM
    Moderator
  • Hi Roy,

    thank you for your fast and helpful response.

    Switching to Windows.Web.Http would probably mean to re-implement the client code manually and not to use WSDL import function for code generation? Or can code generation in Studio be configured to use Windows.Web.Http instead of System.ServiceModel.ClientBase?

    Another option: RFC 7617 (2015) has added a 'charset' auth-param to the basic auth scheme (as hint) which server can sent during challenge. Does System.ServiceModel.ClientBase comply with RFC 7617 'charset' parameter (and reacts accordingly)?

    I regard the WSDL import function as a highlight in Studio that saved us a lot effort. Would be very pitty not using it.

    Best regards, Holger.








    • Edited by PhoenixHsb Friday, August 9, 2019 6:30 AM
    • Marked as answer by PhoenixHsb Monday, August 12, 2019 5:45 AM
    • Unmarked as answer by PhoenixHsb Tuesday, August 13, 2019 5:38 AM
    Friday, August 9, 2019 6:22 AM
  • Hi,

    I've asked another engineer of our team to take a look at this question. There might be some time delay. Thank you for your patience.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 2:52 AM
    Moderator
  • Hi PhoenixHsb,

    I hope you're doing well. Do you have a small  WCF project I can use to reproduce this, along with a small UWP project? Can you also include the wsdl file that generates for you? 
    If you used any documentation or sample to base this on, please also include a link there.

    Also, can you include a sanitized http trace from fiddler, for example, or a similar tool?

    Please don't include any credentials or other sensitive info in the sample. I look forward to hearing back and let me know if you have any updates, questions, or concerns.


    -David Hollowell (MSFT) Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 15, 2019 5:47 PM
    Moderator