我正在实现一个基本的身份验证处理程序,我在ContinueWith
方法上创建了一个任务结果总是未经授权的401。这个值在哪里设定?我应该提前设置吗?
var resp = await base.SendAsync(request, cancellationToken)
.ContinueWith(
task =>
{
response = task.Result;
if (response.StatusCode == HttpStatusCode.Unauthorized
&& !response.Headers.Contains(BasicAuthResponseHeader))
{
response.Headers.Add(
BasicAuthResponseHeader,
BasicAuthResponseHeaderValue);
}
WasHttpExceptionThrown(response.Content);
return response;
});
答案 0 :(得分:0)
作为一般规则,请使用await
而不是 ContinueWith
:
var response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == HttpStatusCode.Unauthorized
&& !response.Headers.Contains(BasicAuthResponseHeader))
{
response.Headers.Add(
BasicAuthResponseHeader,
BasicAuthResponseHeaderValue);
}
WasHttpExceptionThrown(response.Content);
这可确保您的代码保留在请求上下文中。