Partager via


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.

Default.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<script type ="text/javascript" src="https://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1"></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.SetClientToken(token);
map.LoadMap();
map.
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
}
</script>

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

Default.aspx.cs

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 = "https://common.virtualearth.net/find-30/common.asmx";
        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.

CP

Comments

  • Anonymous
    September 23, 2008
    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 (http://msdn.microsoft.com/en-us/library/cc514438.aspx) to get the object references into your app and then set the URL to the CommonService as per your article. Cheers, Hugh

  • Anonymous
    September 23, 2008
    The comment has been removed

  • Anonymous
    September 23, 2008
    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. Cheers, Hugh

  • Anonymous
    February 20, 2009
    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.  

  • Anonymous
    February 25, 2009
    The comment has been removed