WP7 Getting Unique Device and User

In order for you to get the device unique ID and WL (Windows Live) Anonymous ID you need to first declare a couple of things in your WMAppManifest.xml file.  If you attempt to retrieve this information without these capabilities,  it will throw a UnauthorizedAccessException.  Remember, like all capabilities, the user will be alerted that you require this information before they download your application.  So use this only if you need it.

    <Capabilities>
      ...
      <Capability Name="ID_CAP_IDENTITY_DEVICE"/>
      <Capability Name="ID_CAP_IDENTITY_USER"/>
      ...
    </Capabilities>

Now you can access the the desired properties.  First add a using statement

 

using Microsoft.Phone.info;

 

Then access the properties.

 

public static byte[] GetDeviceUniqueID()
        {
            byte[] result = null;
            object uniqueId;
            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
                result = (byte[])uniqueId;  

            return result;
        }  

public static string GetWindowsLiveAnonymousID()
        {
            string result = string.Empty;
            object anid;
            if (UserExtendedProperties.TryGetValue("ANID", out anid))
            {
                if (anid != null && anid.ToString().Length >= (ANIDLength + ANIDOffset))
                {
                    result = anid.ToString().Substring(ANIDOffset, ANIDLength);
                }
            }

            return result;
        }

Related Posts:



  • Richard Lasjunies

    Hi Daniel,
     
     Don't we need to define somewhere 'ANIDLenght' and 'ANIDOffset'?
     If yes do you have the values?

    Sincerely,

    Richard

  • http://techblog.ginktage.com/2011/10/interesting-net-links-october-7-2011/ Interesting .NET Links – October 7 , 2011 | Tech Blog

    [...] WP7 Getting Unique Device and User – Daniel Egan [...]

  • http://twitter.com/dancolasanti Dan Colasanti

    The values for ANIDLength and ANIDOffset are 32 and 2 respectively.  See here:
    http://msdn.microsoft.com/en-u…

  • PHenry

    If your readers are looking for a proj they can F5, I have one they can take for a test drive.

    http://www.pchenry.com/Home/ta…

    PS  Where are ANIDLength and ANIDOffset getting declared/set?  Are they already done?  Sweet if they are!