【发布时间】:2017-11-17 05:12:39
【问题描述】:
我在 MVC5 ASP.NET 项目中工作,并了解到要从控制器向 WEB API 发送经过身份验证的请求,我可以执行以下操作以向标头添加令牌(使用示例代码):
public static class APICaller
{
// Use a single instance for HttpClient to reduce overhead
private static readonly HttpClient client = new HttpClient();
//Set the Authorization Header
public static string SetHeader( string token )
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
return("Success");
}
}
在 HttpClient 线程安全上以这种方式设置标头吗?鉴于此 HttpClient 只有一个实例,其他用户是否有办法访问相同的令牌?
编辑:
我想再问一个问题,以便更好地了解它的工作原理。每次使用相同的 HttpClient 对象发出请求时,是否需要添加标头?
【问题讨论】: