Xamarin 表单不发送 Authorization 标头 [英] Xamarin forms not sending Authorization header

查看:57
本文介绍了Xamarin 表单不发送 Authorization 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能看起来重复,但在再次提出这个问题之前,我已经尝试了我看到的所有解决方案.

I know this question might seem like a duplicate, but I have tried every solution I saw before asking this question again.

我正在尝试使用我自己用 Django Rest Framework 编写的 REST API,在 Xamarin Forms 中使用 C# HttpClient.正在发送请求,但它不包括所需的授权,以令牌作为方案.

I'm trying to consume a REST API which I wrote myself with Django Rest Framework, using C# HttpClient in Xamarin Forms. The request is being sent but it does not include the required Authorization with Token as scheme.

以下是我尝试过的方法:

Here are the things I've tried:

public async Task<IssueListModelView> FetchIssues(string token, string type)
{
    client.BaseAddress = new Uri(XTMonitorContants.SERVICE_BASE_URL);
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(XTMonitorContants.SERVICE_ISSUES_URL + type.ToLower()));

    request.Headers.Add("Authorization", "Token " + token);

    //client.
    //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token); 
    client.DefaultRequestHeaders.Add("Authorization", "Token " + token);
    var response = client.SendAsync(request);
    var jsonString = response.ContinueWith(t => { return t.Result; }).Result.Content.ReadAsStringAsync().Result;

    IssueListModelView resp = new IssueListModelView();
    if (jsonString != "")
    {
        try
        {
            resp = JsonConvert.DeserializeObject<IssueListModelView>(jsonString);
        }
        catch (Exception)
        {
            resp.error = "Internal Server Error";
        }

    }
    return resp;
}

我也试过不使用HttpRequestMessage直接发送请求,但也给了我与

I have also tried to send the request directly without using HttpRequestMessage, but also gave me the same server response of

未提供身份验证凭据."

"Authentication credentials were not provided."

邮递员提出的请求

GET/api/issues/all HTTP/1.1

GET /api/issues/all HTTP/1.1

主机:example.com

Host: example.com

授权:令牌ffba1c43ac346945b788768e2a428d5e5d3c1fc9

Authorization: Token ffba1c43ac346945b788768e2a428d5e5d3c1fc9

缓存控制:无缓存

邮递员令牌:0c1eb96d-d9d6-a9e7-0cfa-0444fdc0248e

Postman-Token: 0c1eb96d-d9d6-a9e7-0cfa-0444fdc0248e

HttpClient 发出的请求

{Method: GET, RequestUri: 'https://example.com/api/issues/all', Version: 1.1, Content: <null>, Headers: { Authorization: Token ffba1c43ac346945b788768e2a428d5e5d3c1fc9 }}

请注意,我已经使用 Postman 正确测试了 API 并设置了 Swagger,它在两者上都运行良好.

Please note that I've tested the API properly using Postman and also setup Swagger, it works fine on both.

如果有人能帮我一把,我会很高兴,因为我认为我可能遗漏了一些东西

Would be glad if someone could give me a hand here, as I think there's probably something I'm missing out

推荐答案

好的,经过彻底检查后,我发现我的 url 中缺少尾部斜杠,服务器正在从 example.com 重定向我的请求/api/issues/allexample.com/api/issues/all/ 所以它在重定向时没有维护标题.

Okay, after thorough checks, I found out that I was missing a trailing slash in my url, the server was redirecting my request from example.com/api/issues/all to example.com/api/issues/all/ so it wasn't maintaining the header while redirecting.

最后,对于其他可能遇到相同问题或尝试使用 API 的人,您可能需要查看 Flurl,我能够完成整个请求并用一行将 json 直接反序列化为 C# 对象,很酷吧?

Finally, for other people that might run into same problem, or could be trying to consume API, you might want to checkout Flurl, I was able to make the whole request thing and deserialize the json directly to C# object with a single line, cool right?

感谢您的回复@Nkosi

Thanks for your responses @Nkosi

这篇关于Xamarin 表单不发送 Authorization 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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