question

GordonS-9701 avatar image
0 Votes"
GordonS-9701 asked ·

Unique idenifier for device

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

dotnet-xamarinforms
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered ·

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.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.