Reputation: 2132
I'm trying to prevent spam and I want to know:
how can I detect if the request to the API is coming from a mobile device?
Thank you
Upvotes: 2
Views: 7750
Reputation: 11
One option is to write your own user agent parser that figures out if it's a mobile browser or not. That is a HUGE job and one you will have to continually keep expanding as new devices are released. It has all its own problems like you said in the other comment - how do you get a list of the new user agents to include...
Another option is to find some free library written in ASP which will do this work for you. I can't see libraries written in asp.net Core API. If you use one, be making sure that it's regularly updated by the developers and that you keep updating your copy of the library too.
Last option would be to use a user agent parsing API. A good one will give you detailed information about the browser, software and the hardware/software types.
I did a comparison of these for my job a few months ago - https://developers.whatismybrowser.com/api/ looked the best to me - it's platform independent (doesn't matter what language you're writing your system in ASP/C#/Ruby), and is freemium and has an active dev team working on it. also because it's an API, you never have to update your code libraries, it always works on the latest detection of what they have written. We still use it today.
Upvotes: 1
Reputation: 311
The only way to check if the request is coming from a mobile device is by checking the user agent sent with each request. The user agent can be found in HttpContext.Request.Headers['User-Agent']
.
Then compare the user agent value with a list of mobile browsers found at e.g.: https://deviceatlas.com/blog/mobile-browser-user-agent-strings
Upvotes: 3