András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

.NET MAUI: Get unique device and installation ids for your app

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Why unique device and installations identifiers are important?

First, let’s define what we mean by a unique device and installation identifiers. Essentially, these are codes that are assigned to individual devices and installations of an app. They allow developers to track usage on a specific device and identify individual installations of the app. This is important because it allows us to understand how users are interacting with our app and identify patterns in usage. For example, if we notice that a particular device is experiencing a high number of crashes, we can use the device ids to track down the specific device and troubleshoot the issue.

Another important use case for unique device and installation identifiers is providing targeted and personalized content or experiences for users. For example, we can use this information to show users personalized advertisements or to offer them special deals or promotions based on their usage patterns.

In addition to these benefits, device and install ids also play an important role in security and fraud prevention. By tracking the usage of our app on specific devices, we can identify and prevent unauthorized access to the app or service. This can help to protect user data and prevent fraud and other malicious activities.

How to get unique identifiers in .NET MAUI?

On Android, you can get a unique device id from the os with accessing Secure.AndroidId.
On iOS, UIDevice.CurrentDevice.IdentifierForVendor is the solution. It requires platform specific knowledge to access the provider APIs. I’ve extended my banditoth.MAUI.Packages library, so you do not need to worry about having this knowledge, just use the banditoth.MAUI.DeviceId NuGet package.

Once you have installed banditoth.MAUI.DeviceId, you need to initalize  the plugin within your MauiProgram.cs‘s CreateMauiApp method. Use the .ConfigureDeviceIdProvider extension method with the using banditoth.MAUI.DeviceId;

    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })
            .ConfigureDeviceIdProvider();
#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }

Use the code with by resolving an instance of IDeviceIdProvider.

The GetDeviceId method returns an unique device identifier. On Android it serves the data from AndroidId, on iOS and MacCatalyst it uses the IdentifierForVendor. Windows returns the GetSystemIdForPublisher().Idas a string.

The GetInstallationId method generates an unique identifier for the application, which will be stored until the application is being reinstalled, or the application’s data being erased.

10 responses to “.NET MAUI: Get unique device and installation ids for your app”

  1. jiaming Avatar
    jiaming

    Successfully installed and configured bandith.MAUI.DeviceId as instructed, but GetDeviceId() display in the program is invalid

  2. Rick Avatar

    I created an instance of IDeviceIdProvider at the top of my class
    IDeviceIdProvider _DeviceID;

    Then in the code I called _DeviceID.GetDeviceID();

    The code errored out as _DeviceID as being null.

    I tried to add _DeviceID = new(); but the error indicated that this was not available for an abstract class.

    My search of the web has produced no working examples, thus your help is appreciated.

    1. banditoth Avatar
      banditoth

      Hey Rick, thanks for using my packages. Are you sure that you have configured the device id provider in the MauiProgram.cs class?

    2. Denis Avatar
      Denis

      I managed to solve it friend.

      private readonly IDeviceIdProvider _DeviceID;

      public MainPage()
      {
      InitializeComponent();

      _DeviceID = new DeviceIdProvider();

      }

      private void OnCounterClicked(object sender, EventArgs e)
      {
      lblid.Text = _DeviceID.GetDeviceId();

      }

    3. Gaurav Kumar Avatar
      Gaurav Kumar

      I was having same issue.
      I am not sure if what I did was sound or not but it worked for me.
      1. As instructed, I used the .ConfigureDeviceIdProvider extension method with the using banditoth.MAUI.DeviceId;
      2. As suggested I needed to use the code with by resolving an instance of IDeviceIdProvider. I injected it as dependency to my viewModel.

      public partial class LoginVm : BaseVm
      {
      private readonly string _deviceId;
      public LoginVm(IDeviceIdProvider deviceIdProvider)
      {
      _deviceId=deviceIdProvider.GetDeviceId();
      }
      }

  3. Gaurav Kumar Avatar
    Gaurav Kumar

    Still not sure if its right way but looks cleaner with dependency injection…

    //MauiProgram.cs //using banditoth.MAUI.DeviceId;
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp()
    .ConfigureDeviceIdProvider();
    builder.Services.AddSingleton();

    //injecting where i need //using banditoth.MAUI.DeviceId.Interfaces;
    public MainPage(IDeviceIdProvider deviceIdProvider)
    {
    var deviceId = deviceIdProvider.GetDeviceId();
    }

    1. Gaurav Kumar Avatar
      Gaurav Kumar

      builder.Services.AddSingleton();
      should be
      builder.Services.AddSingleton();

    2. Gaurav Kumar Avatar
      Gaurav Kumar

      ***** builder.Services.AddSingleton<IDeviceIdProvider, DeviceIdProvider>();

  4. Petr Novák Avatar
    Petr Novák

    Hi (sorry for my English),
    in MAUI application on Windows 10, method “lblid.Text = _DeviceID.GetDeviceId();” returns always “null”, please help.
    Method “_DeviceID.GetInstallationId();” returns correct result.
    Thanks, Petr (from Czech Republic)

    1. banditoth Avatar
      banditoth

      Hi Petr,

      Please ensure that .ConfigureDeviceIdProvider(); have been called when initializing your MAUI app, and you use the dependency injection in a correct way (by populationg ur class with DI).
      If you have further problems, please reach out by filling a bug ticket on the MAUI.Packages repo @ https://github.com/banditoth/MAUI.Packages/issues

      Thanks,
      bandi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.