繁体   English   中英

验证PostAsync()请求时出错

[英]Error while authentication of a PostAsync() request

我正在尝试使用C#提交multipart / form-data。

我使用以下代码添加身份验证标头:

HttpClient client = new HttpClient();               

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "token value XYZ");

然后,我稍后使用以下代码发布多部分表单:

using (MultipartFormDataContent form = new MultipartFormDataContent())
                {

                    form.Add(new StringContent(options["name"]), "name");
                    form.Add(new StringContent(options["timeIdentifier"]), "timeIdentifier");
                    form.Add(new StringContent(options["timeFormat"]), "timeFormat");
                    var result = client.PostAsync(url, form).Result;
                    sd =await result.Content.ReadAsStringAsync();
                 }

但是在连接到服务器时出现身份验证错误。 请提出补救措施。 谢谢。

您在Authorization标头中缺少Bearer身份验证方案。

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "Bearer <token value XYZ>");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM