【发布时间】:2014-02-26 05:48:28
【问题描述】:
我正在使用 HttpClient 和 HttpServer(内存中)运行集成测试。
当测试运行时,会执行令牌处理程序(消息处理程序),我添加此代码只是为了快速测试:
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// other code removed for brevity...
var principal1 = CreatePrincipal(1, "test");
Thread.CurrentPrincipal = principal1;
return await base.SendAsync(request, cancellationToken);
}
[Authorize]
[HttpGet]
public HttpResponseMessage Get(int id)
{
return Request.CreateResponse(HttpStatusCode.OK, _service.Get(id));
}
当我调试操作的控制器构造函数时,我会执行 base.User.Identity.IsAuthenticated 并将其设置为 TRUE。
我原以为会运行该操作,因为设置了 Thread.CurrentPrincipal。
为什么它不起作用?
【问题讨论】:
标签: web asp.net-web-api asp.net-web-api2 asp.net-authorization