brush51
brush51

Reputation: 5771

How to use basic http authentication on Windows rt- Visual Studio 2012 - C#

how can i use basic http Authentication for a HTTPS URL Windows 8 Store App. I am using Visual Studio 2012, C# and XAML.

Is there any point to keep attention when i use an HTTPS URL?

i have tried these following methods:

#1: ###Code is UPDATED - Final running solution

        private async void HttpClientCall(object sender, RoutedEventArgs e)
    {

        System.Diagnostics.Debug.WriteLine(this.GetType().Name + ": HTTPCLientCall entered");

        //System.Diagnostics.Debug.WriteLine("NetworkConnectivityLevel.InternetAccess: " + NetworkConnectivityLevel.InternetAccess);

        //use this, for checking the network connectivity
        System.Diagnostics.Debug.WriteLine("GetIsNetworkAvailable: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable());

        //var msg = new Windows.UI.Popups.MessageDialog("GetIsNetworkAvailable: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable());
        //msg.ShowAsync();


        HttpClient httpClient = new HttpClient();


        // Assign the authentication headers
        httpClient.DefaultRequestHeaders.Authorization = CreateBasicHeader("username", "password");
        System.Diagnostics.Debug.WriteLine("httpClient.DefaultRequestHeaders.Authorization: " + httpClient.DefaultRequestHeaders.Authorization);


        // Call out to the site
        HttpResponseMessage response = await httpClient.GetAsync("https://URLHere");
        System.Diagnostics.Debug.WriteLine("response: " + response);
        string responseAsString = await response.Content.ReadAsStringAsync();
        System.Diagnostics.Debug.WriteLine("response string:" + responseAsString);

        //WebViewP.Source = new Uri("https://URLHere");
    }




    public AuthenticationHeaderValue CreateBasicHeader(string username, string password)
    {
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);

        String logindata = (username + ":" + password);
        System.Diagnostics.Debug.WriteLine("AuthenticationHeaderValue: " + new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)));

        return new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
    }

i just want to display a HTTPS website where authentication is needed. i am getting a Warning from visual studio and it stop working: An error occurred while sending the request.

´A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll´

Upvotes: 0

Views: 1368

Answers (1)

Cyprien Autexier
Cyprien Autexier

Reputation: 1998

You have a more simple way to perform basic authentication.

  • First create an HttpClientHandler instance.
  • You can set Credentials property of your HttpClientHandler instance according to your needs.
  • Initialize your HttpClient using constructor that accepts an HttpMessageHandler instance.

Upvotes: 1

Related Questions