问题
i am connecting to a server with basic authentication and after that i am calling the URL in Webview with following code:
WebView.Source(new Uri("https:(//UrlHere"));
The Webview Puts up a Login-Window, but i am already logged in. Why is this happening? How can i prevent this?
The way i a authenticate to the server:
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));
}
So how can i solve that?
回答1:
This is a long shot, but you could try setting the credentials on HttpClientHandler and hope that the WebView picks it up.
var handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(username,password);
HttpClient httpClient = new HttpClient(handler);
The problem is the WebView is making it's own independent request, so any requests you make with HttpClient are independent of those made by the WebView
来源:https://stackoverflow.com/questions/19633366/webview-display-loginscreen-again-after-login-via-basic-authentication-with-http