-3

I found various questions regarding this issue, such as this one - here.

Unfortunately, the rudimentary method of checking viewport width etc seems a little blunt force object. I'd prefer a scalpel.

Does anyone have a solid method of distinguishing between mobile and desktop browsers using only C# in Blazor?

My scenario is fairly simple, if the user is operating on a mobile device then I would like them to be redirected to a different page, and for whatever reason I am not able to use JS for this - use your imagination.

Edit: To elaborate on this a little, since I realise the question could do with more detail; I am looking for a solution which doesn't require the page to make custom JS calls from code, and for the C# to work after the page has been loaded. The specific need for a post-load function is not technical, and is so that the detection can be switched on/off by the user after they have loaded the page. I don't know if this is possible, and I suspect from the responses so far that it is not, but my question specifically was whether any further developments had been made on this issue since the question above had been posted.

Edit 2: Further, mobile is broad, I am specifically interested in general information about the device such as touchscreen/pointer, brand (Apple, Samsung etc) and device model.

Kris
  • 36
  • 1
  • 8
  • Does this answer your question? [How do I detect mobile devices in blazor server side application?](https://stackoverflow.com/questions/62270247/how-do-i-detect-mobile-devices-in-blazor-server-side-application) – Jim G. Jun 06 '22 at 02:27
  • 2
    I smell an XY problem. If you read [fundamentally similar questions](https://stackoverflow.com/questions/11381673/detecting-a-mobile-browser), the problem is usually solved by crafting responsive CSS using viewports, @media, and correctly supporting touch, orientation, dpi etc. Plus the browser can always lie about such things. – Wyck Jun 06 '22 at 04:11
  • @JimG. I actually linked the question you recommended in the first line, that does not solve my problem, specifically the difference is that I am unable to use JS due to some scenario specific limitations. All answers I have found or been recommended to detect this so far either rely on the detection being done before the page is loaded or being done with JS. I will edit my question to add more concision. – Kris Jun 07 '22 at 03:08
  • "I need to detect mobile browsers" Why? If you design in a responsive way, based on individual browser features, there's no need to implement completely separate pages for different classes of users. Plus in future if people walk around with huge tablet devices in their pockets, you won't be excluding them. – Jeremy Lakeman Jun 07 '22 at 03:39
  • 1
    @JeremyLakeman The need to justify the scenario is something that I have come to dislike about Stack, 'why' I need to do this is not relevant to the question of whether it can be done in the way described. I am specifically not looking to exclude users, I would just like more control over the information that I have about the users device, at any point while the users device is connecting/connected to the endpoint, preferably in C#. If you don't feel that it can be done in an elegant way without JS then that's okay, I would just prefer to have some consensus on this before I pin myself to it. – Kris Jun 07 '22 at 20:16
  • Is it possible? Any test is going to give you false positives and false negatives, with those exceptions increasing over time as future devices change. I suggest you embrace that chaos and ensure your solution will work regardless. Here on stack overflow, there are many questions asked that are really "XY Problems". A developer has painted themselves into a corner and is attempting to solve a problem they should have avoided ages ago. Asking "Why?" isn't a knee-jerk response, it's the collective experience of the crowd asking you to consider an alternative, or justify why you can't. – Jeremy Lakeman Jun 08 '22 at 02:01
  • @JeremyLakeman sure, my justification is a mixture of things; narrow consensual data collection is becoming a bigger focus for clients, consumers and developers; There are conflicts between the use of custom JS calls and custom security measures that will be in place; Heavy reliance on screen dimensions or aspect to find information about a device is becoming less accurate as more devices are produced to different specifications etc. I haven't been able to find a narrow way to use C# within a Razor Page/Component to collect specific device information, without using JS or performing a reload. – Kris Jun 09 '22 at 00:40
  • Another example, which is slightly more complex is: a user consents to the collection of their device OS (Android/iOS) but not their browser type (Chrome, Edge etc). Is it possible to collect one part of the users data without collecting the other? Inferring information about a user based on ancillary information is becoming less acceptable - using dimensions is one way to guess if they are on mobile or desktop but it isn't the same as asking "are you on mobile or desktop?". Its possible to use a range of A's to infer B's, but you may only be allowed B. – Kris Jun 09 '22 at 00:53
  • Your question is not focussed enough and your answers to questions are contradictory - you say you want to avoid legal problems of collecting data without consent, but still want to collect data without consent. Aside from this you are asking about Blazor Server detecting accurate Client details only in C#, which would be impossible as they run on different hardware with only javascript to connect them. If you had a specific technical problem you might get help. – Mister Magoo May 26 '23 at 08:19
  • @MisterMagoo I don't want to assume which region you're in at the moment, but on your first point, there are different classification of data in Europe. It is against the law to process data which can be used to identify an individual, their location or other sensitive characteristics (without consent). It is not against the law to process data which describes non-sensitive characteristics such as the brand of device they are using. I respect your second point though, at the time I wrote this question last year, my understanding of Blazor needed more development. I have solved my issue now. – Kris Jun 20 '23 at 11:43

2 Answers2

1

have a look at the following library BlazorCurrentDevice

I think, this makes what you are looking for.

Happy coding!

Code4Fun
  • 413
  • 5
  • 17
-1

I'm assuming you're using Blazor Server. If you'd like to redirect a request, I recommend you do it in Middleware. You can use HttpContext to get User Agent and other info, and because it's Middleware, it fires before the Blazor page even starts building.

Bennyboy1973
  • 3,413
  • 2
  • 11
  • 16
  • Is there a way to access the HttpContext for a specific element within a blazor page? I assumed that the HttpContext would be linked to the page as a whole and captured when the SignalR connection is first made? Does it refresh with each new SignalR update? - i.e. my assumption was that it "only" fires before the Blazor page starts building. – Kris Jun 07 '22 at 03:23
  • 1
    I downvoted this because the User-Agent header should not be used like that, and major browser vendors (like Google) deprecated the header ages ago, making it useless for this purpose anyway. – Dai Jun 07 '22 at 03:31
  • @Dai Fair enough. Several people have asked why the OP even WANTS to do this, which I think is an excellent question. And then-- why does he not want to use Javascript? Really, though? So already, we're way out of best practices and into hacky workarounds. – Bennyboy1973 Jun 07 '22 at 04:23
  • @Bennyboy1973 I should stress, while I hope for a solution to the problem, I would be happy enough to turn around to my client and express that the facilitation of their request is not possible. I would rather not do this unless it's more or less true that it isn't possible. The reasons behind the request aren't overly relevant to the question, which is whether since the linked question (2020) was asked, has there been any progress on enabling more concise detections directly from C# without the need for JS. This can be answered in binary fashion although I would appreciate elaboration. – Kris Jun 07 '22 at 20:01
  • @Kris It very much matters why they want to do this. If they are basing their request on a lack of knowledge, setting them straight may improve their experience. If they are worried about size of display, then css covers that VERY well now. If they expect workers using mobiles to do different tasks, then they can use roles or claims to determine what workers see. Depending on information coming INTO the server from some unknown browser is pretty dodgy-- it's easily hacked, and unreliable. – Bennyboy1973 Jun 07 '22 at 23:43
  • @Bennyboy1973 the why is due to various advances in existing and anticipated legislation - such as GDPR in Europe. It is becoming less acceptable to query information about a user and their device prior to connection, and we have seen shifts in this area already with (for example) cookie policies. This problem is distinct from cookies though, because asking to place something on a users machine is different to asking to take information from a users machine. The legal limitation is "on user request only", but the technical limitation is "with C#". – Kris Jun 09 '22 at 00:19
  • @Bennyboy1973 if I have to do this on page load, my option will most likely be to gather the required information prior to loading the page, after presenting the user with a page which requests their consent. This requires two loads though, with data collection sandwiched in-between. Having a component or piece of C# that collects the data when a button is clicked would be more efficient, and the data I am focussing on at the moment is primarily Android vs iOS, version number, browser type and so on. I had hoped this would be possible with C# compiled WASM, perhaps in hybrid for example. – Kris Jun 09 '22 at 00:27
  • Try this: the login / front page has 2 "Enter site" buttons, with display set on or off depending on CSS breakpoints. So the user will see what SEEMS to be the same button however they access the site, but actually the buttons will navigate to different pages and / or set variables in a Scoped service that will determine how subsequent pages are displayed. This is equivalent to detecting mobile, but without the very bad practice of actually detecting mobile. – Bennyboy1973 Jun 09 '22 at 03:33