2016-11-26 47 views
0

我正在使用web api mvc 5,我在做api調用日誌記錄。以下是一個片段在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

http://stackoverflow.com/a/18384253/4753489 –

回答

0

假設你的令牌標頭,如授權密鑰來,那麼你可以從請求的頭部拉承載令牌:

if (Request.Headers.Contains("Authorization")) 
     var bearerToken = Request.Headers.GetValues("Authorization").FirstOrDefault();