Unique idenifier for device

Gordon S 501 Reputation points
2021-03-05T11:11:07.6+00:00

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)?

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

Accepted answer
  1. JarvanZhang 23,961 Reputation points
    2021-03-05T12:56:05.097+00:00

    Hello,​

    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  
       DependencyService.Get<IGetDeviceInfo>().GetDeviceID();  
    

    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.

1 additional answer

Sort by: Most helpful
  1. Victor Rangel 11 Reputation points
    2022-08-17T02:38:59.347+00:00

    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

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.