如何使用HttpClient发布身份验证

Jes*_*sse 26 .net c# httpclient

我正在尝试使用HttpClient在C#中执行以下curl(适用于我).

curl -X POST http://www.somehosturl.com \
     -u <client-id>:<client-secret> \
     -d 'grant_type=password' \
     -d 'username=<email>' \
     -d 'password=<password>' \
     -d 'scope=all
Run Code Online (Sandbox Code Playgroud)

C#代码:

HttpClientHandler handler = new HttpClientHandler { Credentials = new  
            System.Net.NetworkCredential ("my_client_id", "my_client_secret")
    };


    try
    {
        using(var httpClient = new HttpClient(handler))
        {
            var activationUrl = "www.somehosturl.com";

            var postData = "grant_type=password&username=myemail@myemail.com&password=mypass&scope=all";
            var content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");

            var response = await httpClient.PostAsync(activationUrl, content);
            if(!response.IsSuccessStatusCode)
                return null;

            var result = await response.Content.ReadAsStringAsync();

            return result;
        }
    }
    catch(Exception)
    {
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

执行时,只是崩溃,甚至没有捕获异常

通常我能够完全获得GET和POST,但最让我失望的是如何设置auth的东西(client-id和client-secret)

Ale*_*ler 23

首先,你必须Authorization用你的<clientid>和设置-Header <clientsecret>.

而不是使用StringContent你应该使用FormUrlEncodedContent如下所示:

var client = new HttpClient();
client.BaseAddress = new Uri("http://myserver");
var request = new HttpRequestMessage(HttpMethod.Post, "/path");

var byteArray = new UTF8Encoding().GetBytes("<clientid>:<clientsecret>");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("grant_type", "password"));
formData.Add(new KeyValuePair<string, string>("username", "<email>"));
formData.Add(new KeyValuePair<string, string>("password", "<password>"));
formData.Add(new KeyValuePair<string, string>("scope", "all"));

request.Content = new FormUrlEncodedContent(formData);
var response = await client.SendAsync(request);
Run Code Online (Sandbox Code Playgroud)

  • 你应该解释*为什么*这是问题的答案.您是否想说除了必须设置身份验证标头之外,OP还会以错误的方式传递数据? (2认同)

n.y*_*nko 21

尝试将您的凭据直接放入HttpClient的headers属性中.

using (var client = new HttpClient()) {
       var byteArray = Encoding.ASCII.GetBytes("my_client_id:my_client_secret");
       var header = new AuthenticationHeaderValue("Basic",Convert.ToBase64String(byteArray));
       client.DefaultRequestHeaders.Authorization = header;

       return await client.GetStringAsync(uri);
}
Run Code Online (Sandbox Code Playgroud)


Сер*_*гей 5

请参阅BasicAuthenticationHeaderValue

httpClient.DefaultRequestHeaders.Authorization
    = new BasicAuthenticationHeaderValue("login", "password");
Run Code Online (Sandbox Code Playgroud)

或者使用 IdentityModel 的扩展:

<PackageReference Include="IdentityModel" Version="4.0.0" />

var client = new HttpClient();
client.SetBasicAuthentication(login, password);
client.SetBearerToken(token);
Run Code Online (Sandbox Code Playgroud)

  • 您已经撰写了有关“BasicAuthenticationHeaderValue”的文章,但提供了“AuthenticationHeaderValue”的链接,因此您的示例在 .Net 5 中无效。 (4认同)