Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting browser information from IOwinContext

How can I get the browser from the IOwinContext?

I am attempting to log requests alongside 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); //???
}
like image 444
Haroon Avatar asked Dec 14 '22 14:12

Haroon


1 Answers

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

context.Request.Headers.Get("User-Agent")
like image 101
Richard Avatar answered Dec 27 '22 10:12

Richard