-->

ASP.NET MVC - How to detect if user is using a scr

2019-07-17 08:51发布

问题:

So I'm currently trying to check if a user is using a screen reader on our site. The reason I would like to check if they are is because our site provides a training module in which if they are using a screen reader, I would like to show a button that would allow them to download a printable version of the training.

Here is what I have tried so far:

internal class UnsafeNativeMethods
    {
        public const uint SPI_GETSCREENREADER = 0x0046;

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);
    }

    public static class ScreenReader
    {
        public static bool IsRunning
        {
            get
            {
                bool returnValue = false;
                if (!UnsafeNativeMethods.SystemParametersInfo(UnsafeNativeMethods.SPI_GETSCREENREADER, 0, ref returnValue, 0))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "error calling SystemParametersInfo");
                }
                return returnValue;
            }
        }
    }

I snagged this code from the following StackOverflow thread: C# : How to detect if screen reader is running?

The method I'm using apparently works for some as you can see on this thread, but I'm currently always getting "false" when calling ScreenReader.IsRunning. I'm storing the ScreenReader.IsRunning value in a ViewBag and then in my view I'm using razor syntax to show a button if that value is true. Not really sure why its always false. I have tested this using JAWS as well as Narrator that comes on everyone's computer if you are using Windows 10 I think. Any help would be much appreciated. Thank you all!

回答1:

It's not possible to detect whether a user is running a screenreader. If it were possible, it would open a door for unscrupulous types to home in on people with disabilities, or for a totally different "accessible" browser experience to be delivered, rather than building interfaces that are inclusive by design.

You could create a button that's placed off-screen but still in the tab order, and make it visible when it receives focus. Then blind users could find it, and sighted keyboard users wouldn't think something strange had happened when the button received focus.



回答2:

I think you can only check if user has a chrome extensions or something like that working on client side with javascript.

Idea: You can create an exe or a bath file for check users system and send information about this to your server. If user download this file and there is no screen reader then load the page. Otherwise give error about the screen reader. And when response come from user computer maybe you can use SignalR for show content to user. You can make this file a must works in users system and start downloading this file when user load the page. Its not a good way but maybe you can check this with this way.



回答3:

The HttpRequest.Browser property returns a HttpBrowserCapabilities object that enlists the capabilities of the device that has made the request. Bear in mind that ASP.NET uses the User-Agent string sent as part of the HTTP request to identify a client. Then to populate the HttpBrowserCapabilities properties, ASP.NET processes the user-agent string using a set of pre-installed browser files, which are contained in the following location: %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\CONFIG\Browsers

HttpBrowserCapabilities Class

There are also third-parties which are far more detailed and updated with detecting device info, i.e. 51Degrees