Skip to content

Commit

Permalink
[MAUI] Fixed issue that caused the API key to be dropped (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
duffh authored May 19, 2023
1 parent e0df709 commit ba73b3d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
13 changes: 12 additions & 1 deletion src/MAUI/Maui.Samples/CategoryPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ArcGIS.Helpers;
using ArcGIS.Samples.Managers;
using ArcGIS.Samples.Shared.Managers;
using ArcGIS.Samples.Shared.Models;
using ArcGIS.ViewModels;
using CommunityToolkit.Maui.Views;
Expand Down Expand Up @@ -90,6 +91,16 @@ protected override void OnAppearing()
}

SetBindingContext();

}

protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);

// Ensure we enable the API key when we navigate here at any time.
if (ApiKeyManager.KeyDisabled)
{
ApiKeyManager.EnableKey();
}
}
}
11 changes: 5 additions & 6 deletions src/MAUI/Maui.Samples/Helpers/SampleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ public static async Task LoadSample(SampleInfo sampleInfo)
// Set the currently selected sample.
SampleManager.Current.SelectedSample = sampleInfo;

// Restore API key if leaving named user sample.
if (_namedUserSamples.Contains(SampleManager.Current?.SelectedSample?.FormalName))
{
ApiKeyManager.EnableKey();
}

// Remove API key if opening named user sample.
if (_namedUserSamples.Contains(sampleInfo.FormalName))
{
ApiKeyManager.DisableKey();
}
else
{
// Ensure the API key is enabled if the sample that is being opened is not a named user sample.
ApiKeyManager.EnableKey();
}

// Clear any existing credentials from AuthenticationManager.
foreach (Credential cred in AuthenticationManager.Current.Credentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using ArcGIS.Samples.Shared.Managers;
using ArcGIS.Helpers;
using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Mapping;
Expand Down Expand Up @@ -170,11 +169,5 @@ private void ListCloseClicked(object sender, EventArgs e)
{
MapsListBorder.IsVisible = false;
}

public void Dispose()
{
// Re-enable the API key in the viewer when exiting this sample.
ApiKeyManager.EnableKey();
}
}
}
9 changes: 1 addition & 8 deletions src/MAUI/Maui.Samples/Samples/Security/OAuth/OAuth.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using ArcGIS.Samples.Shared.Managers;
using ArcGIS.Helpers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
Expand All @@ -21,7 +20,7 @@ namespace ArcGIS.Samples.OAuth
instructions: "When you run the sample, the app will load a web map which contains premium content. You will be challenged for an ArcGIS Online login to view the private layers. Enter a user name and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). If you authenticate successfully, the traffic layer will display, otherwise the map will contain only the public basemap layer.",
tags: new[] { "OAuth", "OAuth2", "authentication", "cloud", "credential", "portal", "security" })]
[ArcGIS.Samples.Shared.Attributes.ClassFile("Helpers/ArcGISLoginPrompt.cs")]
public partial class OAuth : ContentPage, IDisposable
public partial class OAuth : ContentPage
{
// - The URL of the portal to authenticate with
private const string ServerUrl = "https://www.arcgis.com/sharing/rest";
Expand Down Expand Up @@ -61,11 +60,5 @@ private async Task Initialize()
await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
}
}

public void Dispose()
{
// Re-enable the API key in the viewer when exiting this sample.
ApiKeyManager.EnableKey();
}
}
}
2 changes: 2 additions & 0 deletions src/Samples.Shared/Managers/ApiKeyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static class ApiKeyManager
private static string _key;
private static bool _keyDisabled;

public static bool KeyDisabled { get { return _keyDisabled; } }

// Name for file on windows systems. / Name for key in .NET MAUI SecureStorage.
private const string _apiKeyFileName = "agolResource";

Expand Down

0 comments on commit ba73b3d

Please sign in to comment.