2

I need to display a few of webpages from a remote Apache webserver for the IoT Core app I'm developing. I've used the WebView class method .Navigate() for non-protected pages and It works really neat. However, for my project, I need to login first into the webserver (username + password) and then retrieve the info from the pages, but I don't know how to do it using the WebView class. I'm clueless.

I found a solution that uses the WebBrowser class Navigate(), sending the credentials as a 64 base encoded string, but the Navigate() of WebView only allows 1 argument, the URI, and nothing else. I can't seem to find WebBrowser class neither.


I've already tried to embed the username/password into the URI but It's not working properly and I'm aware It's not a good idea to do it so.

Is it possible to achieve this using WebView?Any suggestions/ideas?

Any help appreciated


Edit: I've found a solution that works well for my problem, I'm posting it in case it can be of help to someone with similar issues.

Uri req_uri = new Uri(uri_list[i]); 

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();  
filter.ServerCredential = new PasswordCredential(req_uri.ToString(), "username", "password");

 HttpCookieCollection cookiejar = filter.CookieManager.GetCookies(req_uri);  
 if (cookiejar.Count > 0)  
 {
     foreach (HttpCookie cookie in cookiejar)
     {
         filter.CookieManager.SetCookie(cookie);
     }

 } 
 Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);


  Windows.Web.Http.HttpRequestMessage http_req = new Windows.Web.Http.HttpRequestMessage();

  http_req.Method = Windows.Web.Http.HttpMethod.Get;   
  http_req.RequestUri = req_uri;
  var clientResponse = client.GetAsync(req_uri);

  webView.NavigateWithHttpRequestMessage(http_req);

  http_req.Dispose();
  client.Dispose();

2 Answers2

2

You might me able to use the NavigateWithHttpRequestMessage which would allow you to navigate to a page using a HttpRequestMessage.

You would still need to authenticate out-of-band, using HttpClient, obtain the cookies and authentication headers, then use them when you build your HttpRequestMessage.

Here's a StackOverflow that should help with that

Community
  • 1
  • 1
AlexDrenea
  • 7,981
  • 1
  • 32
  • 49
  • I've actually managed to solve it after several tries following your approach along with the use of HttpBaseProtocolFilter, which works as HttpClientHandler for the UWP Windows.Web.Http namespaces and can hold the auth credentials within (ServerCredential) for the login. Much appreciated. – A. Anders Martin Jul 11 '16 at 09:24
-2

If your remote web server supports HTTP Basic authentication you can pass the credentials in the URL like so:

https://Username:Password@www.example.com/index.html
Ian Hoppes
  • 252
  • 1
  • 4