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.


Share on twitter Share on facebook Share on linkedin Share on linkedin