Converting ANID (WP7) to ANID2 (WP8)

Posted by on

Your Windows Phone 7 app uses ANID and you want to upgrade to Windows Phone 8.1 WinRT or native Javascript?
Short answer is you can't.

What am I even doing here?
As the title suggests the answer is to update to either 8.0 or 8.1 sliverlight and for the love of god store your ids locally.

In Windows Phone 7 there was a property we could use called ANID UserExtendedProperties.GetValue("ANID") this gave you a user specific id based on the logged in Microsoft account (across devices).

Then in Windows Phone 8 ANID was phased out and instead we were given ANID2 UserExtendedProperties.GetValue("ANID2") which is a more specific id based on the publisher id of the application. This was done to improve privacy so no entity could track a user across devices and applications.

Now with Windows Phone 8.1 Universal apps we get the the WinRT xaml stack and native javascript apps!
EVERYTHING IS AWESOME!
Wait, didn't you say you can't upgrade?
Unfortunately moving to Universal (Either Xaml or Javascript) means you lose access to the Phone APIs such as Microsoft.Phone.Info.UserExtendedProperties so no access to ANID or ANID2. So we need to stay on Silverlight for now.

This is where I got to before cursing the world and asking myself why I didn't store the id locally. Store your ids locally!

After coming to the realisation that I couldn't update to a native javascript app I started looking at how to go about the ANID to ANID2 upgrade process. I came across this article from the Windows blog http://blogs.windows.com/buildingapps/2013/03/11/migrating-anids-to-windows-phone-8/. Only to realise I was truncating the ANID because thats how the site did it where I got the code.

Don't worry if you are as well, here is how to fix it.
If you are truncating the ANID with Substring(2, 32) then all you need to do is append "A=" to the start and "&E=f48&W=3" to the end and the converter will work for it.

The conversion can also be done in C# with a few lines of code:

//Put your publisher id here
var publisherId = new Guid("000000000-0000-0000-0000-000000000000");

var anidAsBytes = System.Text.Encoding.Unicode.GetBytes(anid);
var publisherAsBytes = publisherId.ToByteArray();
var macObject = new HMACSHA256(anidAsBytes.Take(anidAsBytes.Length / 2).ToArray());
var hashedBytes = macObject.ComputeHash(publisherAsBytes);

var result = Convert.ToBase64String(hashedBytes);