Unique idenifier for device

Gordon S 496 Reputation points

I am new to Xamarin Forms development and new to this forum, so sorry if this is a stupid question! I am working on a Xamarin Forms app that is using oAuth style of login / authentication. I can pass a "device id" when requesting a token, so that the token applies to that device only, for the specified user. Is there an easy way to obtain a unique id via Xamarin Forms, or do I need to generate one (somehow) and store it on the device (somehow)?

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,014 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,876 Reputation points


    Welcome to our Microsoft Q&A platform!

    Is there an easy way to obtain a unique id via Xamarin Forms

    To get the idenifier for device, try to achieve the function on the native platform using DependencyService.

       //1.create an interface to define the method  
       public interface IGetDeviceInfo  
           string GetDeviceID();  
       //2.implement the service on the android platform  
       [assembly: Xamarin.Forms.Dependency(typeof(GetInfoImplement))]  
       namespace TestApplication_4.Droid  
           public class GetInfoImplement : IGetDeviceInfo  
               string IGetDeviceInfo.GetDeviceID()  
                   var context = Android.App.Application.Context;  
                   string id = Android.Provider.Settings.Secure.GetString(context.ContentResolver, Secure.AndroidId);  
                   return id  
       //3.consume the DependencyService command in the shared project  

    For the iOS platform, you could use the following method.

       string id = UIDevice.CurrentDevice.IdentifierForVendor.AsString();  

    Best Regards,

    Jarvan Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Victor Rangel 1 Reputation point

    Watch out. string id = UIDevice.CurrentDevice.IdentifierForVendor.AsString() is not a fixed value, it changes depending on the installation, in fact, every time you reinstall the application the value will change

    0 comments No comments