从IOwinContext获取浏览器信息

时间:2015-04-29 10:54:05

标签: .net asp.net-mvc owin

如何从IOwinContext获取浏览器?

我正在尝试将请求与我的owin中间件(下面的代码)的响应一起记录。

public async override Task Invoke(IOwinContext context)
{
    var sw = new Stopwatch();
    sw.Start();
    var user = context.Authentication.User.Identity.IsAuthenticated ?
        context.Authentication.User.Identity.Name :
        "anonymous";

    _logger.WriteVerbose(
        string.Format("{0} {1} '{2}{3}{4}' @ {5} for {6}",
        context.Request.Scheme,
        context.Request.Method,
        context.Request.PathBase,
        context.Request.Path,
        context.Request.QueryString,
        context.Request.LocalIpAddress,
        user));
     await Next.Invoke(context);

    _logger.WriteVerbose(
        string.Format("{0} {1} {2}ms - {3}",
        context.Response.StatusCode,
        context.Request.Path,
        sw.ElapsedMilliseconds,
        context.Request.Browser); //???
}

1 个答案:

答案 0 :(得分:11)

您可以从请求中获取User-Agent标头:

context.Request.Headers.Get("User-Agent")