Retrieving Browser Information from IOwinContext

How can I get a browser from IOwinContext?

I am trying to log requests along with their responses from my owin middleware (code below).

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); //???
}
+4
source share
1 answer

You can get the User-Agent header from the request:

context.Request.Headers.Get("User-Agent")
+11
source

All Articles