UIWebView and basic authentication in MonoTouch

1.7k views Asked by At

In a MonoTouch project I have a UIWebView loading a Basic authentication protected url. I managed to authenticate against the first request to the url by manually adding the basic auth header:

UIWebView webView = new UIWebView(View.Frame);
... initialize webView
NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl("http://example.com"));
string header = "Basic " + Convert.ToBase64String (Encoding.UTF8.GetBytes("domain\\username:password"));
request["Authorization"] =  header;
webView.LoadRequest(request);`

It works well in the first page that is opened in the UIWebView. But when a href link is clicked inside the webview, the request fails with 401. I examined the sent headers via Fiddler and my header is not sent in any subsequent request after the first one. I tried injecting the header to all requests in the ShouldStartLoad delegate but there was no result:

webView.ShouldStartLoad = (w, request, navType) => {
    ((NSMutableUrlRequest)request)["Authorization"] = header;
    return true;
};

How should I inject the Authorization http header to ALL requests in a UIWebView?

0

There are 0 answers