Trying to get the user-agent from request in asp.net web api self host

后端 未结 5 1849
轻奢々
轻奢々 2021-02-05 00:21

I\'m trying to get the user-agent in a web api self host and I\'m either doing it wrong, or the web api itself is altering the user agent string.

I\'ve tried using sever

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 00:45

    var context = new HttpContextWrapper(HttpContext.Current);
    HttpRequestBase request = context.Request;
    var browserdetail = request.UserAgent;
    

    This worked for me if you want only browser name then simply write:

    var browserdetail = request.browser
    

    And if you want clients ip address then simply do:

    var browserdetail = request.hostaddress and use it for generating token key for authenticaton.

提交回复
热议问题