gpt4 book ai didi

C# - HttpRequest 请求第二次登录凭据

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:44 25 4
gpt4 key购买 nike

我正在开发 Windows 通用应用程序。在这个应用程序中,我必须在 webview 中显示一个需要身份验证才能访问的网站。我通过以下方式访问它:

    Uri url = new Uri("https://ssl.mywebsite.nl/");
HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(url);
http.Credentials = new NetworkCredential(userName, userPassword);
HttpWebResponse response = (HttpWebResponse)await http.GetResponseAsync();
webView.Source = response.ResponseUri;

这在 Windows Phone 上运行得非常好。但是,在 Windows 上,我会看到一个要求我登录的屏幕:

"The server ssl.mywebsite.nl is asking for your username and password. The server reports that it is from Please Log In."

这是 Windows 8.1 中的错误还是我遗漏了一些非常明显的东西?

最佳答案

所以我认为它在 Windows Phone 上运行但在 Windows 桌面上运行不运行的原因是因为它在 Windows Phone 上缓存凭据而不是在 Windows 上。这导致 future 在 Windows Phone 上成功进行身份验证尝试。由于我尝试在 WebView 中显示检索到的信息的方式意味着它必须重新验证自己,导致在 Windows Phone 上成功显示但在 Windows 上显示失败(或者更确切地说是验证问题)。

解决方法:

string encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
.GetBytes(userName + ":" + userPassword));
HttpRequestMessage mess = new HttpRequestMessage(HttpMethod.Get,
new Uri("https://ssl.mywebsite.nl/"));
mess.Headers.Add("Authorization", "Basic " + encoded);
webView.NavigateWithHttpRequestMessage(mess);

此方法允许请求始终使用给定的凭据,因此不要求用户提供它们。

关于C# - HttpRequest 请求第二次登录凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com