Search code examples
c#xamarinxamarin.formsxamarin.androidxamarin.ios

Secure localisation app Xamarin.Forms , Xamarin.Ios , Xamarin.Android


Good morning , I am developing a xamarin application which allows you to locate its user.

Given the importance of the user's location, I must reassure myself of the use of the exact location, i.e. avoid that:

  • Do not mislead the application with a fake GPS application (justify Location of xamarin essentials which allows you to manage this)

  • That the user cannot install the software on the PC emulators.

  • That the user cannot inject code to avoid this verification.

Are there any solutions in xamarin allowing me to resolve these problems in a secure and efficient manner in order to achieve my objectives (have a reliable location).

Thanks in advance


Solution

  • You can use the Xamarin.Essentials: Geolocation. It has the method to detecte Mock Locations.

    var request = new GeolocationRequest(GeolocationAccuracy.Medium);
    var location = await Geolocation.GetLocationAsync(request);
    
    if (location != null)
    {
        if(location.IsFromMockProvider)
        {
            // location is from a mock provider
        }
    }
    

    That the user cannot install the software on the PC emulators.

    First, you can add permission to read the file system in your AndroidManifest.xml.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    Second, Create a method to read the contents of '/proc/cpuinfo':

    using System.IO;
    using System.Text;
    
    public string GetCpuInfo()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            using (StreamReader sr = new StreamReader("/proc/cpuinfo"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    sb.AppendLine(line);
                }
            }
        }
        catch (IOException ex)
        {
            // Handle exception
        }
    
        return sb.ToString();
    }
    

    Then, you can call this method get and display CPU information.

    string cpuInfo = GetCpuInfo();