UIWebView and basic authentication in MonoTouch

In a MonoTouch project, I have a UIWebView loading the authenticated base URL. I managed to authenticate against the first request to the URL by manually adding the main 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 on the first page that opens in UIWebView. But when the href link was clicked in the web browser, the request failed with 401. I looked at the sent headers via Fiddler, and my header is not sent to any subsequent request after the first. I tried to insert a header for all requests of the ShouldStartLoad delegate, but there was no result:

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

How do I add an http authorization header for ALL requests in a UIWebView?

+3
source share

All Articles