Getting the users anonymous Live ID on Windows Phone 7

While working on a Reversi game for Windows Phone 7, I needed a good way to identify the user. The idea is to let users challenge their friends by entering their name. On the one hand, anybody with a Windows Phone 7 should be able to play. On the other hand, I don’t want to be in the identity business. So the obvious choice is to use Live ID. You have to use Live ID to activate the phone, so every one of my users would have one.

A quick search reveals that there is an API to obtain the user’s anonymous Live ID. Unfortunately, more vigorous searching does not reveal what an “anonymous” Live ID is. If it’s an ID, how can it be anonymous?

Here is the sample code from the MSDN site:

string anid = UserExtendedProperties.GetValue("ANID") as string;
string anonymousUserId = anid.Substring(2, 32);

This code raises a number of questions for me:

  • What is an anonymous Live ID?
  • Is the length always 32?
  • What are those first two characters you’re skipping?

To find some answers, I ran this code in the emulator. If you’ve tried it, you know that it throws a NullReferenceException. UserExtendedProperties.GetValue(“ANID”) returns null. So you’re stuck. Unless you know someone with a WP7 device.

And I do.

Chris Koenig was kind enough to run the sample code on an actual phone. Here is what the API returns (I’ve anonymized his anonymous ID by scrambling the characters).

? anid

"A=2E434B328BC68118DB640915FFFFFFFF&E=a45&W=1"

? anonymousUserId

"2E434B328BC68118DB640915FFFFFFFF"

So now we have an answer to the questions. The anonymous Live ID is a GUID with no punctuation. It is unique to the user without telling an outside observer with no other information who it is. Yes, it is always 32 characters (since it’s a GUID). And the first two characters are the name half of a name/value pair.

Not wanting to assume that the character position will never change, I modified the parse code. Here’s a parser that will extract the value of “A” in an ampersand-delimited list of name/value pairs.

public static string Parse(string anid)
{
    string[] parts = anid.Split('&');
    IEnumerable<string[]> pairs = parts.Select(part => part.Split('='));
    string id = pairs
        .Where(pair => pair.Length == 2 && pair[0] == "A")
        .Select(pair => pair[1])
        .FirstOrDefault();
    return id;
}

The plan is to prompt the user for an alias the first time they use the app. I’ll send the anonymous ID and the alias to my server, which will ensure that the alias is not already taken and associate the two. Then other users can challenge them using that alias. I get the best of both worlds. I can rely upon Microsoft to be the identity provider, and I can let my users challenge each other with their own names.

Thanks, Chris!

2 Responses to “Getting the users anonymous Live ID on Windows Phone 7”

  1. Michael Says:

    Very nice, clean and straight to the point. Why does the alias need to be unique if you can always get the Live Id? Is the alias being used as the main "login" or simply a display name? Is this alias simply a friendly way to ensure the person receiving a challenge from a friend can distinguish between "Michael" and "Michael2". Or is there some technical reason this is done that my puny brain is not processing?

  2. Michael L Perry Says:

    No, there's no technical reason. It's just so that if someone challenges "Michael", then the challenge goes to the right person. If you choose the alias "Michael" and I do too, then the system would not know which one of us your friend is challenging.

Leave a Reply

You must be logged in to post a comment.