【问题标题】:HTTP Request with Basic Auth always returns 401具有基本身份验证的 HTTP 请求始终返回 401
【发布时间】:2016-11-18 01:59:34
【问题描述】:

我正在尝试在 UWP (Windows 10) 应用中执行 GET。我尝试了几种方法,但总是返回 401。

在 Postman 中它运行良好,但我似乎无法让它在我的应用程序中运行。我错过了什么。

这些是我尝试过的方法(都返回401):

方法一:

        var request = WebRequest.Create("http://api.fos.be/person/login.json?login=200100593&password=pass");
        request.Headers["Authorization"] = "Basic MYAUTHTOKEN";
        var response = await request.GetResponseAsync();

方法二:

        const string uri = "http://api.fos.be/person/login.json?login=200100593&password=pass";
        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.Credentials = new System.Net.NetworkCredential("MYUSERNAME", "MYPASSWORD");
        using (var client = new HttpClient(httpClientHandler))
        {
            var result = await client.GetAsync(uri);
            Debug.WriteLine(result.Content);

        }

方法三:

        var client = new RestClient("http://api.fos.be/person/login.json?login=200100593&password=pass");
        var request = new RestRequest(Method.GET);
        request.AddHeader("postman-token", "e2f84b21-05ed-2700-799e-295f5470c918");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("authorization", "Basic MYAUTHTOKEN");
        IRestResponse response = await client.Execute(request);
        Debug.WriteLine(response.Content);

第三种方法是直接从 Postman 生成的代码,那么为什么它可以在那里工作而不是在我的应用程序中?

【问题讨论】:

  • @Aaron 提供的可能是正确的解决方案,同时请确认usernamepassword 是正确的。

标签: c# windows uwp windows-10 windows-10-universal


【解决方案1】:

This 线程帮助我找到了解决方案。我使用的是 http://,但我必须将其设为 https://。使用该线程中的代码的 HTTPS 是解决方案。

这是我的最终代码:

    public static async void GetPerson()
    {
        //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("MYUSERNAME", "MYPASS");
        System.Diagnostics.Debug.WriteLine("httpClient.DefaultRequestHeaders.Authorization: " + httpClient.DefaultRequestHeaders.Authorization);
        // Call out to the site
        HttpResponseMessage response = await httpClient.GetAsync("https://api.fos.be/person/login.json?login=usern&password=pass");
        System.Diagnostics.Debug.WriteLine("response: " + response);
        string responseAsString = await response.Content.ReadAsStringAsync();
        System.Diagnostics.Debug.WriteLine("response string:" + responseAsString);
    }
    public static 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));
    }

【讨论】:

    【解决方案2】:

    我会先试试这个:

    检查您的“MYAUTHTOKEN”,它通常是用户名:密码的组合,并且是 base 64 编码的。因此,如果您的用户名是“user”,密码是“pass”,则需要 base64 编码“user:pass”

    var request = WebRequest.Create("https://api.fos.be/person/login.json");
    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Text.Encoding.UTF8.GetBytes("user:pass"));
    var response = await request.GetResponseAsync();
    

    【讨论】:

    • 这不起作用,它给出以下错误:“发生安全问题。(来自 HRESULT 的异常:0x800C000E)”
    • @Aaron 在使用基本身份验证时不应该删除?login=200100593&password=pass 吗?
    • @ThomasSchneiter,是的,我认为这是不必要的。我会相应地编辑帖子。
    • @vixez 啊,显然 WebRequest 类将不支持 uri 中的凭据。我已经更新了答案。
    • 好的,谢谢,但是服务器需要参数。同时我也找到了答案。我已经发布了答案,但直到明天才能接受它作为答案。还是谢谢。
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2016-11-07
    • 1970-01-01
    • 2019-09-11
    • 2011-07-31
    相关资源
    最近更新 更多