我有以下代码结构:
[BasicAuthentication]
public class MessagesController : ApiController
{
[HttpGet]
public Enumerable<IListItemModel> Get()
{
// Thread.CurrentPrincipal is WindowsPrincipal, not custom IPrincipal
}
}
public class BasicAuthenticationAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
...
// set thread principal to some custom Principal
Thread.CurrentPrincipal = user;
if (HttpContext.Current != null)
{
HttpContext.Current.User = Thread.CurrentPrincipal;
}
base.OnActionExecuting(actionContext);
}
}
但是如果我使Get方法异步/等待线程主体保持自定义Principal(据我所知它正在从HttpContext恢复)。
它发生在我的localhost(Windows 10)上,并且从未在生产(Windows Server)上重现。
这是预期的行为吗?在没有异步方法的情况下,保持校长安全的最佳方法是什么?