To associate a database file on the
server with the user of the device, the app retrieves the user’s
Windows Live anonymous ID from the Microsoft.Phone.Info.UserExtendedProperties
class. The Windows Live anonymous ID is a representation of the user’s
Windows Live ID that does not include any user identifiable
information. When a user activates a device, he must provide a Windows
Live ID. The Windows Live anonymous ID lets you identify the user by
his Windows Live account, without actually seeing his details, and
there is no way to correlate the anonymous ID with the Windows Live ID.
When retrieving the anonymous ID from UserExtendedProperties
by way of the ANID2 key value (see Listing 1), the resulting string is the hashed representation of the anonymous ID.
Note
New to Windows Phone 8 is the ANID2 property
that is a hashed version of the Windows Phone 7.5 ANID property. ANID2
is hashed with your publisher ID to ensure that different publishers
cannot track users across different apps. You can no longer use the
ANID property to retrieve the Windows Live anonymous ID in Windows
Phone 8. ANID can be accessed only from Windows Phone OS 7.0 and
Windows Phone OS 7.1 apps that use the Microsoft Advertising SDK for
Windows Phone.
LISTING 1. DeviceProperties
Class
public class DeviceProperties : IDeviceProperties
{
/// <summary>
/// Gets the windows live anonymous ID.
/// This method requires ID_CAP_IDENTITY_USER
// to be present in the capabilities of the WMAppManifest.
/// </summary>
/// <returns>The string id for the user.</returns>
static string GetWindowsLiveAnonymousId()
{
object result;
UserExtendedProperties.TryGetValue("ANID2", out result);
return result != null ? result.ToString() : null;
}
string windowsLiveAnonymousId;
public string WindowsLiveAnonymousId
{
get
{
return windowsLiveAnonymousId
?? (windowsLiveAnonymousId = GetWindowsLiveAnonymousId());
}
}
}
Tip
It is better to associate data with the user
of the device rather than the device itself. By relying on the ID of
the user, rather than the ID of the device, you can provide a greater
level of assurance that if the phone changes ownership, the new user of
the phone will not have access to previous owner’s data.
Retrieving the anonymous ID from the UserExtendedProperties
does not work on the emulator. For testing purposes, the IDeviceProperties
implementation can be swapped with a mock implementation that retrieves a predefined anonymous ID.