通过使用HttpClient的基本身份验证登录后,WebView再次显示Loginscreen [英] WebView display Loginscreen again after login via basic authentication with HttpClient

查看:91
本文介绍了通过使用HttpClient的基本身份验证登录后,WebView再次显示Loginscreen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基本身份验证连接到服务器,然后使用以下代码在Webview中调用URL:

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"));  

Webview放置进入登录窗口,但我已经登录了。为什么发生?
如何防止这种情况发生?

The Webview Puts up a Login-Window, but i am already logged in. Why is this happening? How can i prevent this?

ia对服务器进行身份验证的方式:

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));
}

那么我该如何解决呢?

推荐答案

这是一个很长的尝试,但是您可以尝试在HttpClientHandler上设置凭据,并希望WebView能够使用它。

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);

问题是WebView发出了自己的独立请求,因此您使用HttpClient发出的任何请求都是独立的WebView制作的图片中的

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

这篇关于通过使用HttpClient的基本身份验证登录后,WebView再次显示Loginscreen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆