我正在使用web api mvc 5,我正在进行api呼叫记录。以下是一个片段
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var corrId = string.Format("{0}{1}", DateTime.Now.Ticks, Thread.CurrentThread.ManagedThreadId);
var requestInfo = string.Format("{0};{1}", request.Method, request.RequestUri);
var requestMessage = await request.Content.ReadAsByteArrayAsync();
await IncommingMessageAsync(corrId, requestInfo, requestMessage);
var response = await base.SendAsync(request, cancellationToken);
byte[] responseMessage;
if (response.IsSuccessStatusCode)
responseMessage = await response.Content.ReadAsByteArrayAsync();
else
responseMessage = Encoding.UTF8.GetBytes(response.ReasonPhrase);
await OutgoingMessageAsync(corrId, requestInfo, responseMessage);
return response;
}
我想知道的是,如果请求来自经过身份验证的请求,我怎么能获得持票人令牌?我试过HttpContext.Current.User,但它总是为空。
提前致谢。
答案 0 :(得分:0)
假设您的令牌作为授权密钥进入标头,那么您可以从请求的标头中提取承载令牌:
if (Request.Headers.Contains("Authorization"))
var bearerToken = Request.Headers.GetValues("Authorization").FirstOrDefault();