【发布时间】:2018-06-06 17:52:41
【问题描述】:
我在尝试访问 TFS 中的提交时收到了以下输出: uri 在我的浏览器中有效,但身份验证不适用于我的 httpClient。我的用户名是域\用户,现在我只使用用户名的用户部分,但我已经尝试过使用和不使用域。请帮忙!
System.Net.Http.HttpRequestException:响应状态码不表示成功:401(未授权)。 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
try
{
var httpClient = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetStringAsync(uri);
Console.WriteLine(response);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
【问题讨论】:
标签: authentication tfs httpclient