我在中间件中具有以下Invoke
函数,用于记录Web api请求。它基于this example,并且工作正常。但是,我也想保存发送请求的当前用户的username
。
public async Task Invoke(HttpContext httpContext,IApiLogService apiLogService)
{
try
{
_apiLogService = apiLogService;
var request = httpContext.Request;
if (request.Path.StartsWithSegments(new PathString("/api")))
{
var stopWatch = Stopwatch.StartNew();
var requestTime = DateTime.UtcNow;
var requestBodyContent = await ReadRequestBody(request);
var originalBodyStream = httpContext.Response.Body;
await SafeLog(requestTime,stopWatch.ElapsedMilliseconds,200,//response.StatusCode,request.Method,request.Path,request.QueryString.ToString(),requestBodyContent
);
}
else
{
await _next(httpContext);
}
}
catch (Exception ex)
{
await _next(httpContext);
}
}
但是,User
的{{1}}属性似乎缺少用户信息。下面我分享其内容。我想知道如何获取发送请求的用户的ID。我没有使用Windows身份验证。有什么建议吗?