
Once you have created a Bing Maps developer account, you can access Bing services. See the Bing Maps overview topic in this help system for instructions on obtaining an account. To access Bing services you must generate a token, which stores credential information, and provide it in each request to a Bing service. As a result, Bing components built for the ArcGIS Silverlight API require you to provide a token when constructing the component (whether in XAML or code-behind). To generate the token you must submit your Bing Maps developer account ID and password to the Bing Maps Token Service. Defining user credentials within the Silverlight application is not supported or recommended since they are easily viewed by clients. Instead you can create the token manually and add it to the Bing components or automate token creation by utilizing server-side code to generate and pass a token to the ArcGIS Silverlight application.
To generate a token manually using a Web page, see the information available on a pre-created token page. The following steps outline a programmatic server-side solution for generating and passing a token to a Silverlight application.
| Staging Environment |
|---|
| https://staging.common.virtualearth.net/find-30/common.asmx?wsdl |
| Production Environment |
|---|
| https://common.virtualearth.net/find-30/common.asmx?wsdl |
To access the Production environment, you must be a licensed customer. Contact the Microsoft Bing Maps Licensing Team for more information.
<script language="C#" runat="server">
bool useStagingServer = true;
private string BingAccountID = "121212";
private string BingAccountPassword = "mypassword";
public string bingInfo;
protected void Page_Load(object sender, EventArgs e)
{
BingApp.Web.TokenService.CommonService commonService =
new BingApp.Web.TokenService.CommonService();
string bingToken = null;
//Use staging server instead of production server
if (useStagingServer)
{
commonService.Url = "http://staging.common.virtualearth.net/find-30/common.asmx";
}
else
{
commonService.Url = "http://common.virtualearth.net/find-30/common.asmx";
}
commonService.Credentials = new System.Net.NetworkCredential(BingAccountID, BingAccountPassword);
BingApp.Web.TokenService.TokenSpecification tokenSpec = new BingApp.Web.TokenService.TokenSpecification();
// Set the token specification properties
tokenSpec.TokenValidityDurationMinutes = 480; //8 hours is the maximum allowed
if (System.Web.HttpContext.Current != null && !System.Web.HttpContext.Current.Request.IsLocal)
{
tokenSpec.ClientIPAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
}
else
tokenSpec.ClientIPAddress = "127.0.0.1";
try
{
bingToken = commonService.GetClientToken(tokenSpec);
}
catch (Exception ex)
{
string error = ex.Message;
}
// Define initialization parameters for Silverlight application startup object
bingInfo = string.Format("token={0},useStaging={1}", bingToken, Convert.ToString(useStagingServer));
}
</script>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/BingApp.xap" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="initParams" value="<% =bingInfo %>" />
</object>
// Store a reference to the Bing Services token string
public string BingToken { get; set; }
public bool UseStagingServer { get; set; }
private void Application_Startup(object sender, StartupEventArgs e)
{
//Get token generated on the server (aspx) and provided as an initialization parameter
BingToken = e.InitParams["token"];
// Set whether to use staging or production server in the aspx
UseStagingServer = bool.Parse(e.InitParams["useStaging"]);
this.RootVisual = new Page();
}
<esri:Map x:Name="MyMap" Loaded="MyMap_Loaded"> <esri:Map.Layers> <ve:TileLayer ID="BingLayer" LayerStyle="AerialWithLabels"/> </esri:Map.Layers> </esri:Map>
private void MyMap_Loaded(object sender, RoutedEventArgs e)
{
// Associate Bing Services token provided as initialization parameter to application with
// the Bing tile layers defined in page xaml.
foreach (Layer layer in MyMap.Layers)
if (layer is TileLayer)
{
(layer as TileLayer).Token = (Application.Current as BingApp.App).BingToken;
if ((Application.Current as BingApp.App).UseStagingServer)
(layer as TileLayer).ServerType = ServerType.Staging;
else
(layer as TileLayer).ServerType = ServerType.Production;
}
To view a complete solution, download the code for the Bing Maps sample.