【问题标题】:httpclient token Authorization has been denied for this requesthttpclient token 此请求的授权已被拒绝
【发布时间】:2019-03-11 14:17:19
【问题描述】:

我在使用基于令牌的身份验证创建的 MVC 网站上有一个 API。这在我的一个应用程序上效果很好,但在另一个应用程序上我收到“此请求的授权已被拒绝。”。

我得到令牌很好,但在拨打电话时出现上述错误。

这是我创建的一个测试。

class TestApi
{
    private const string baseAddress = "http://localhost:50485";

    private const string baseApiAddress = baseAddress + "/api/DojoDbApi";
    async Task<string> GetToken(string userName, string password)
    {
        var keyValues = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("username", userName),
            new KeyValuePair<string, string>("password", password),
            new KeyValuePair<string, string>("grant_type", "password")
        };
        var request = new HttpRequestMessage(HttpMethod.Post, "/oauth/token") { Content = new FormUrlEncodedContent(keyValues) };
        var client = new HttpClient { MaxResponseContentBufferSize = 256000, BaseAddress = new Uri(baseAddress) };

        var response = await client.SendAsync(request).ConfigureAwait(false);
        var content = await response.Content.ReadAsStringAsync();
        JObject jwtDynamic = JsonConvert.DeserializeObject<dynamic>(content);
        var accessToken = jwtDynamic.Value<string>("access_token");
        Debug.WriteLine(accessToken);

        return accessToken;
    }
    public async Task<string> GetHello(string userName, string password)
    {


        var accessToken = await GetToken(userName, password);
        var client = new HttpClient { MaxResponseContentBufferSize = 256000, BaseAddress = new Uri(baseApiAddress) };
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            // Add the Authorization header with the AccessToken.
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = await client.GetAsync(new Uri(baseApiAddress + "/Hello"));
            var s = await response.Content.ReadAsStringAsync();
            Debug.WriteLine(s);
            return s;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"              ERROR {0}", ex.Message);
            return ex.Message;
        }
    }

}

真正让我感到困惑的是,我可以使用 Delphi 应用程序非常愉快地访问 API。

【问题讨论】:

  • 两个应用程序在同一台服务器上运行?两者有什么区别?
  • 发生在我的开发系统上。两者都托管在 Azure 上,但无论在哪里运行都没有区别。
  • 在 StackOverflow 的其他地方看到过类似的东西,但没有一个答案有帮助:-(
  • 调试问题的最佳方法是使用像wireshark或fiddler这样的嗅探器。将 Delphi 应用程序中的请求标头与 c# 应用程序中的请求标头进行比较。然后让 c# headers 看起来像 Delphi headers。
  • 我们说话的时候只是在玩 WireShark。

标签: c# httpclient


【解决方案1】:

一直在与其他开发者讨论这个问题(在 ChesterDevs 聚会中)。

这与 cookie 有关。

如果您附加 ?AspxAutoDetectCookieSupport=1 yo 调用它就可以了。

如果您将网络配置中的 cookieless 更改为“UseCookies”,则它可以正常工作。

How to remove AspxAutoDetectCookieSupport=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2018-10-05
    • 2016-12-06
    相关资源
    最近更新 更多