Authentication and Tokens with Virtual Earth

So, there's an MSDN technical article posted online showing users how to authenticate and grab tokens for their Virtual Earth application - Implementing Customer Identification. Microsoft Virtual Earth authentication is required for (1) Enterprise Customers with a Virtual Earth license; (2) access to the staging platform; (3) access to certain features like traffic overlays and extracting route geometry.

I found some of these things didn't work as I thought they should, so I started troubleshooting. I added my ASMX web reference and found CommonServiceSoap() didn't exist (a MapPoint Web Service relic), so I changed it to CommonService() and it worked fine. If you're having issues getting tokens, this is likely why. Also, I should mention if you're getting a 401 Unauthorized, it means your account doesn't have access to the service you're requesting - so make sure (a) your account is valid - go to the customer service site for this; or, (b) ensure you have the right level of permissions in your account for accessing the services you want.

Here's the code that worked for me.


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <script type ="text/javascript" src=""></script>
    <script type="text/javascript">
        var map = null;
        var token = "<%=strClientToken %>";

        // Loads and displays the map
        function GetMap()
            // Set the token before loading the map or making any other
            // Virtual Earth requests.
            map = new VEMap('mymap');
            map.AttachEvent('ontokenexpire', MyHandleTokenExpire);
            map.AttachEvent('ontokenerror', MyHandleTokenError);

        function MyHandleTokenExpire()
           // insert code here to handle token expiration

        function MyHandleTokenError()
           // insert code here to handle token errors

    <title>CP's Authentication Page</title>
<body onload="GetMap();">
    <div id='myMap' style="position: relative; width:640; height:480"></div>


using VEWSStaging;

public partial class _Default : System.Web.UI.Page
    public string strClientToken;
    protected void Page_Load(object sender, EventArgs e)
        CommonService commonService = new CommonService();
        commonService.Url = "";
        commonService.Credentials = new System.Net.NetworkCredential("XXXX","YYYY");

        // Create the TokenSpecification object to pass to GetClientToken.
        TokenSpecification tokenSpec = new TokenSpecification();

        // Use the Page object to retrieve the end-client’s IPAddress.
        tokenSpec.ClientIPAddress = Page.Request.UserHostAddress;

        // The maximum allowable token duration is 480 minutes (8 hours).
        // The minimum allowable duration is 15 minutes.
        tokenSpec.TokenValidityDurationMinutes = 480;

        // Now get a token from the Virtual Earth Platform Token service.
        strClientToken = commonService.GetClientToken(tokenSpec);


That should get you authenticated in the case that you are an Enterprise Customer, want access to the staging platform, need access to certain features like traffic overlays and extracting route geometry or need access to future services that will require authentication.


Comments (5)

  1. Hugh@minus34 says:

    Hi Chris,

    Under some circumstances, you can’t create a web reference for CommonService ASMX in Visual Studio via HTTP or HTTPS.  I had it working on my Vista Laptop, but not on my XP desktop…

    The workaround is to add the MapPoint Web Service web reference ( to get the object references into your app and then set the URL to the CommonService as per your article.



  2. Chris Pendleton says:

    Hugh – Great tip! Although Vista vs. XP shouldn’t make a difference. Not sure why this happened to you. Perhaps different versions of VS?


  3. Hugh@minus34 says:

    They were both VS2008, and we had some hits and misses with our dev guy’s machines too.  It seems to be a obscure TCP/IP or proxy issue.  One of your guys was chasing it up for us.



  4. ErinG says:

    Sorry to retry an old thread…

    I am having the same problem with the token service.  I cannot connect to it.  I get a 400 bad request error.

    I can connect no problem via IE.  I am prompted for credentials, enter them and the page loads.  

    I am behind a firewall, using a proxy server, but I thought VS used the settings from IE for proxy servers and trusted sites, etc.  I cannot understand why referencing the service in IE works, but VS 2008 does not.  

Skip to main content