9

在使用 ConfigureAwait(false) 的引用库中使用 Thread.CurrentPrincipal 的声明会造成任何问题,还是 ExecutionContext 的逻辑调用上下文的流动会在那里照顾我?(到目前为止,我的阅读和测试表明它会)。

示例 WebAPI 控制器操作:

[CustomAuthorizeThatSetsCurrentUsersClaimsToThreadCurrentContextAndHttpContextCurrentUser]
public async Task<Order> Get(int orderId)
{
    return await _orderBusinessLogicLibrary.LoadAsync(orderId); // defaults to .ConfigureAwait(true)
}

来自外部引用库的示例加载函数:

[ClaimsPrincipalPermission(
    SecurityAction.Demand,
    Operation="Read",
    Resource="Orders")]
[ClaimsPrincipalPermission(
    SecurityAction.Demand,
    Operation="Read",
    Resource="OrderItems")]
public async Task<Order> Load(int orderId)
{
    var order = await _repository.LoadOrderAsync(orderId).ConfigureAwait(false);

    // here's the key line.. assuming this lower-level function is also imposing
    // security constraints in the same way this method does, would
    // Thread.CurrentPrincipal still be correct inside the function below?
    order.Items = await _repository.LoadOrderItemsAsync(orderId).ConfigureAwait(false);
    return order;
}

此外,答案不能是“那么不要使用 ConfigureAwait(false)!”。这可能会导致其他问题,例如死锁(不要阻塞异步代码)。

4

1 回答 1

13

根据我的测试,Thread.CurrentPrincipal即使您使用ConfigureAwait(false). 以下 WebAPI 代码设置主体,然后阻止async调用,强制另一个线程恢复该async方法。那个其他线程确实继承了正确的主体。

private async Task<string> Async()
{
    await Task.Delay(1000).ConfigureAwait(false);
    return "Thread " + Thread.CurrentThread.ManagedThreadId + ": " + Thread.CurrentPrincipal.Identity.Name + "\n";
}

public string Get(int id)
{
    var user = new ClaimsPrincipal(new ClaimsIdentity(
        new[]
        {
            new Claim(ClaimTypes.Name, "Bob"),
        }
    ));
    HttpContext.Current.User = user;
    Thread.CurrentPrincipal = user;

    var ret = "Thread " + Thread.CurrentThread.ManagedThreadId + ": " + Thread.CurrentPrincipal.Identity.Name + "\n";

    ret += Async().Result;

    return ret;
}

当我在 IISExpress 的新实例上运行此代码时,我得到:

"Thread 7: Bob\nThread 6: Bob\n"

但是,我应该指出,ConfigureAwait(false)不建议使用以避免死锁。在 ASP.NET 上尤其如此。如果可能的话,使用ConfigureAwait(false) 一直使用async。请注意,WebAPI 是全async栈的,您应该能够做到这一点。

于 2013-12-10T03:13:54.390 回答