Friday, March 02, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

Who Are You?

May 18

Written by:
Wednesday, May 18, 2011 9:05 PM  RssIcon

Do you have a WP7 app that talks to a server?  Are you trying to figure out a unique way to identify your users?  Well, besides asking them I mean (which may NOT be so unique)?image  I think I can help you out.

Go back, way back, waaaaaaay back to the days before unified passwords and Hotmail Sign In, you had to register and create brand new accounts on practically every website you wanted to get something from.  Right?  Remember those days?  Oh, that was just last week eh?  DOH!

Ok, now, try it out on your cell phone!  DOH!  Ya, smaller keyboard, and try typing out your “Very strong password” with 8 alpha-numeric-with-special-characters validated with regex patterns to help keep it secure………Nasty eh?

Ya, I thought so to.  So what are you going to do to help out your users?  Well, first, you have to figure out, do you REALLY need to know their username?  Do you REALLY need their password?  Will some other way to identify your users suffice?  I think there is.

imageThe Windows Phone has a few APIs you can use to get back unique IDs which you can use to uniquely identify your users if you need that type of information to save stuff on a server via web services.  The magic here is, the person using the phone, IS the person you think they are, cause, well, they’re using the phone.  Ya, ya, phone’s get stolen, but so do car keys, computers, and even identities.  For a smartphone application, I think these APIs are good enough, we’re not protecting Fort Knox after all.

Ok, so what’s the deal?  First off, you’re looking for UserExtendedProperties.  Look to the right for Windows Live Anonymous ID, you’ll see two lines, one is the raw string returned from the API call, and one is the substring with the actual “real” ID.  I know, I know, I also thought this should be as simple to use as DateTime.Now, but hey, that’s what this blog is about.

The quick’n’dirty call is this,

UserExtendedProperties.TryGetValue( "ANID", out windowsAnonymousIDObject );

So the magic here is knowing the property name you need to get the “Windows Live Anonymous ID” is “ANID”.  Then, once you get that object, you need to strip out the middle 30 characters (from 2 to 32).

string windowsAnonymousID = windowsAnonymousIDObject.ToString().Substring( 2, 32 );

Seems very weird I know, but check out MSDN, they have the substring, 2 to 32 in there two.  On the right, you’ll see what I got on my actual phone (well, blurred a bit cause it is my identity after all LOL).  But I did leave in both the raw string returned and the substring to show you both.

Oh, and that is what you’ll see on your phone.  “But Peter, what will I see in my emulator?”  GREAT question.  Check it out.  Hhhmmm not much there for the anonymous ID eh?  Interesting.  Oh, that (Unknown) is my code, not what’s returned back from the API.


You’ll also notice in my code sample, I also have a few other properties from DeviceExtendedProperties as well.  In there, my example pulls back and displays the

  • Device name, which isn’t what YOU called it in Zune when you first set it up, but more like the product SKU name from the manufacturer.
  • Manufacturer name, this is something that the emulator does display but it’s certainly different from the actual device.
  • Device Unique ID, which is like the anonymous ID, but different, it’s tied to the phone, so if you repave the phone to give to someone else, this value should NOT change.  Therefore, if you device some funky license-scheme using this value, WATCH OUT cause you could hose someone in the future who inherits the phone.

The magic to using the device unique ID is you get back a byte array.  Ya, weird I know, but here’s the code.

object deviceUniqueID; 
byte[] deviceID = null; 
if( DeviceExtendedProperties.TryGetValue( "DeviceUniqueId", out deviceUniqueID ) ) 
  deviceID = (byte[])deviceUniqueID; 
string deviceIDString = Convert.ToBase64String( deviceID ); 
DeviceUniqueIDTextBlock.Text = deviceIDString;

If you’re playing around with these things on your phone, you’re done with this blog, so long, hope you come back again real soon!  But if you’re using this in a real live app you’re going to submit to the MS Marketplace, then you’ll need one more piece to this puzzle.  MS application certification requirements dictate, if you’re going to use these APIs, you have to declare you’re going to use them.  And the way you do that is by having the following lines in your WMAppManifest.xml in the Properties folder.  (sorry for the screen cap instead of code, but the XML doesn't play nice with this blog engine).


Once you do that, you will be able to uniquely identify your user and the phone they’re on.  If you wanted to use this information to save…..say a profile on a central server, you COULD ask them to log into that website and link these IDs to those login creds and then you can easily sync up information to the website for viewing on their 20” monitor.  Cool option.

Ok, now that you know how to uniquely identify your users, it’s time to grab a coffee and get coding!



Source Code:

MSDN: UserExtendedProperties

MSDN: DeviceExtendedProperties

Wikipedia: Password strength

Nick Harris.NET: Windows Phone 7 – How to find the device unique id windows live anonymous Id and manufacturer

Location: Blogs Parent Separator TechTidBits

1 comment(s) so far...


Re: Who Are You?

Thanks to John Marshall, he pointed out I was using some deprecated methods. I just updated the code in source control. The older/deprecated stuff is there, just commented out (for comparison's sake). The kicker is, the one I was HOPING for to be updated (get the Windows Live annonymous ID) wasn't updated. Yup, you still have to do the object, substring dance to get it to work.

By Peter Henry on   Wednesday, October 12, 2011 9:53 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement