GotDibbs
GotDibbs

Reputation: 3167

Load a webpage that requires basic authentication in a Windows 8 App

How would you load an external site that requires basic authentication inside an iframe in a Windows 8 HTML/JavaScript app?

I've found that you can't use URLs of the form http://username:password@host/.

Upvotes: 0

Views: 988

Answers (1)

GotDibbs
GotDibbs

Reputation: 3167

In order to load the URL I was trying to load, I had to use a webview or x-ms-webview and invoke it's navigateWithHttpRequestMessage function in order to pass the basic authentication header.

This resulted in the following code structure:

function ready(element, options) {
    var webview = document.querySelector('[data-hook~="camera"]');

    webview.navigateWithHttpRequestMessage(buildRequest());
}

function buildRequest() {
    var username = 'username',
        password = 'password',
        Crypto = Windows.Security.Cryptography,
        Http = Windows.Web.Http,
        request,
        buffer,
        token;

    request = new Http.HttpRequestMessage(
        Http.HttpMethod.get,
        new Windows.Foundation.Uri('http://ipcamera/'));

    buffer = Crypto.CryptographicBuffer.convertStringToBinary(
        [username, password].join(':'),
        Crypto.BinaryStringEncoding.utf8);
    token = Crypto.CryptographicBuffer.encodeToBase64String(buffer);

    request.headers.authorization = new Http.Headers.HttpCredentialsHeaderValue('basic', token);

    return request;
}

The caveat to this is that none of the DOM ready events seem to fire, so if you need to interact with the external site, you'll have to play with timers.

Upvotes: 1

Related Questions