Hmm, that sounds a bit weird. If you already have authenticated and you can access the portal with given credentials then it sounds that for some reason that isn't attached correctly or there are other privileges that doesn't match.
You should see the error in the results object for the basemap, can you confirm that? Or is the await actually throwing an exception?
How do you provide the credential for GenerateOfflineMapJob? OfflineMapTask (and job) doesn't support setting credentials directly since the task might be accessing multiple domain so you have to hook into AuthenticationManager. I added a simple example to this response.
I assume that you are using a basemap from the basemap gallery which requires you to authenticate the user since using the export tiles operation is shared as subscriber content. The operation doesn't use credits but requires the user be authenticated. If you don't use our basemaps and those services exposes export tiles operation and none of the feature services requires authentication, you shouldn't see that prompted.
Here is an example how to hook authentication to the job.
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.Security;
using Esri.ArcGISRuntime.Tasks.Offline;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WaterNetworkDemo
{
public partial class MainWindow : Window
{
private const string PORTAL_ITEM_ID = "19df6a8e2c694a3c958778ac25398f26";
private string offlineDataFolder;
public MainWindow()
{
InitializeComponent();
AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(CreateKnownCredentials);
Initialize();
}
private async void Initialize()
{
try
{
Map offlineMap = null;
MobileMapPackage offlineMapPackage = null;
offlineDataFolder = Path.Combine(Directory.GetCurrentDirectory(), "OfflineMaps", "WaterNetwork.mmpk");
try
{
offlineMapPackage = await MobileMapPackage.OpenAsync(offlineDataFolder);
offlineMap = offlineMapPackage.Maps.First();
}
catch (FileNotFoundException)
{
}
if (offlineMapPackage == null)
{
loadingIndicator.Visibility = Visibility.Collapsed;
busyIndicator.Visibility = Visibility.Visible;
Directory.CreateDirectory(offlineDataFolder);
offlineMap = await GenerateOfflineMapAsync(offlineDataFolder, PORTAL_ITEM_ID);
}
loadingIndicator.Visibility = Visibility.Collapsed;
busyIndicator.Visibility = Visibility.Collapsed;
if (offlineMap != null)
{
MyMapView.Map = offlineMap;
}
else
{
mapNoAvailable.Visibility = Visibility.Visible;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async Task<Map> GenerateOfflineMapAsync(string path, string webmapItemId)
{
Map offlineMap = null;
try
{
#region 1) Create an offline map task
ArcGISPortal portal = await ArcGISPortal.CreateAsync();
PortalItem webmapItem = await PortalItem.CreateAsync(portal, webmapItemId);
var areaOfInterest = GeometryEngine.Project(webmapItem.Extent, SpatialReferences.WebMercator);
OfflineMapTask task = await OfflineMapTask.CreateAsync(webmapItem);
#endregion
#region 2) Specify parameters
GenerateOfflineMapParameters parameters =
await task.CreateDefaultGenerateOfflineMapParametersAsync(areaOfInterest);
parameters.MinScale = 200000;
parameters.MaxScale = 1250;
#endregion
#region 3) Generate offline map, report progress and use offline map
var job = task.GenerateOfflineMap(parameters, path);
job.ProgressChanged += (s, e) =>
{
Dispatcher.Invoke(() =>
{
Percentage.Text = job.Progress > 0 ? $"{job.Progress.ToString()} %" : string.Empty;
});
};
GenerateOfflineMapResult results = await job.GetResultAsync();
offlineMap = results.OfflineMap;
#endregion
}
catch (TaskCanceledException)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Taking map offline failed.");
}
return offlineMap;
}
private string _username = "username";
private string _password = "password";
private async Task<Credential> CreateKnownCredentials(CredentialRequestInfo info)
{
Credential knownCredential = null;
try
{
knownCredential = await AuthenticationManager.Current.GenerateCredentialAsync
(info.ServiceUri,
_username,
_password,
info.GenerateTokenOptions);
}
catch (Exception ex)
{
MessageBox.Show("Access to " + info.ServiceUri.AbsoluteUri + " denied. " + ex.Message, "Credential Error");
}
return knownCredential;
}
}
}
<Window x:Class="WaterNetworkDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
xmlns:local="clr-namespace:WaterNetworkDemo"
xmlns:converters="clr-namespace:WaterNetworkDemo.DataConverters"
mc:Ignorable="d"
Title="MainWindow" Height="525" Width="790">
<Grid>
<esri:MapView x:Name="MyMapView"/>
<TextBlock x:Name="mapNoAvailable" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18"
Text="Map isn't currently available." Visibility="Collapsed"></TextBlock>
<Grid x:Name="busyIndicator" Background="#807f7f7f" Visibility="Collapsed">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Foreground="White" Margin="10" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="Generating offline map... "></Run>
<Run x:Name="Percentage" Text=""></Run>
</TextBlock>
<ProgressBar x:Name="progressBar" Minimum="0" Maximum="100"
IsEnabled="True" IsIndeterminate="True" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Height="10" Margin="0,0,0,10"/>
</Grid>
</Grid>
<Grid x:Name="loadingIndicator" Background="#807f7f7f" Visibility="Visible">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Loading online map..." Foreground="White" Margin="10" FontSize="18"/>
<ProgressBar IsEnabled="True" IsIndeterminate="True" Width="100" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"/>
</Grid>
</Grid>
</Grid>
</Window>