forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenSecuredChallenge.cs
259 lines (222 loc) · 11.5 KB
/
TokenSecuredChallenge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2017 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "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 Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Security;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using System.Threading.Tasks;
using UIKit;
namespace ArcGISRuntimeXamarin.Samples.TokenSecuredChallenge
{
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
"ArcGIS token challenge",
"Security",
"This sample demonstrates how to authenticate with ArcGIS Server using ArcGIS Tokens to access a secure service. Accessing secured services requires a login that's been defined on the server.",
"1. When you run the sample, the app will load a map that contains a layer from a secured service.\n2. You will be challenged for a user name and password to view that layer.\n3. Enter the correct user name (user1) and password (user1).\n4. If you authenticate successfully, the secured layer will display, otherwise the map will contain only the public layers.",
"Authentication, Security, ArcGIS Token")]
[Register("TokenSecuredChallenge")]
public class TokenSecuredChallenge : UIViewController
{
// Hold a reference to the MapView.
private MapView _myMapView;
// Public and secured map service URLs.
private const string PublicMapServiceUrl = "https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
private const string SecureMapServiceUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA_secure_user1/MapServer";
// Public and secured layer names.
private const string PublicLayerName = "World Street Map - Public";
private const string SecureLayerName = "USA - Secure";
// Use a TaskCompletionSource to store the result of a login task.
private TaskCompletionSource<Credential> _loginTaskCompletionSource;
// Labels to show layer load status.
private UILabel _publicLayerLabel;
private UILabel _secureLayerLabel;
public TokenSecuredChallenge()
{
Title = "Token Challenge";
}
private void Initialize()
{
// Define a challenge handler method for the AuthenticationManager.
// This method handles getting credentials when a secured resource is encountered.
AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(CreateCredentialAsync);
// Create the public layer and provide a name.
ArcGISTiledLayer publicLayer = new ArcGISTiledLayer(new Uri(PublicMapServiceUrl))
{
Name = PublicLayerName
};
// Create the secured layer and provide a name.
ArcGISMapImageLayer tokenSecuredLayer = new ArcGISMapImageLayer(new Uri(SecureMapServiceUrl))
{
Name = SecureLayerName
};
// Track the load status of each layer with a LoadStatusChangedEvent handler.
publicLayer.LoadStatusChanged += LayerLoadStatusChanged;
tokenSecuredLayer.LoadStatusChanged += LayerLoadStatusChanged;
// Create a new map and add the layers.
Map myMap = new Map();
myMap.OperationalLayers.Add(publicLayer);
myMap.OperationalLayers.Add(tokenSecuredLayer);
// Add the map to the map view.
_myMapView.Map = myMap;
}
// Handle the load status changed event for the public and token-secured layers.
private void LayerLoadStatusChanged(object sender, Esri.ArcGISRuntime.LoadStatusEventArgs e)
{
// Get the layer that triggered the event.
Layer layer = (Layer) sender;
// Get the label for this layer.
UILabel labelToUpdate;
if (layer.Name == PublicLayerName)
{
labelToUpdate = _publicLayerLabel;
}
else
{
labelToUpdate = _secureLayerLabel;
}
// Create the text string and font color to describe the current load status.
string updateText = layer.Name;
UIColor textColor = UIColor.Gray;
switch (e.Status)
{
case Esri.ArcGISRuntime.LoadStatus.FailedToLoad:
updateText = layer.Name + " (Load failed)";
textColor = UIColor.Red;
break;
case Esri.ArcGISRuntime.LoadStatus.Loaded:
updateText = layer.Name + " (Loaded)";
textColor = UIColor.Green;
break;
case Esri.ArcGISRuntime.LoadStatus.Loading:
updateText = layer.Name + " (Loading ...)";
textColor = UIColor.Gray;
break;
case Esri.ArcGISRuntime.LoadStatus.NotLoaded:
updateText = layer.Name + " (Not loaded)";
textColor = UIColor.LightGray;
break;
}
// Update the layer label on the UI thread.
BeginInvokeOnMainThread(() =>
{
labelToUpdate.Text = updateText;
labelToUpdate.TextColor = textColor;
});
}
// AuthenticationManager.ChallengeHandler function that prompts the user for login information to create a credential.
private async Task<Credential> CreateCredentialAsync(CredentialRequestInfo info)
{
// Return if authentication is already in process.
if (_loginTaskCompletionSource != null && !_loginTaskCompletionSource.Task.IsCanceled)
{
return null;
}
// Create a new TaskCompletionSource for the login operation.
// Passing the CredentialRequestInfo object to the constructor will make it available from its AsyncState property.
_loginTaskCompletionSource = new TaskCompletionSource<Credential>(info);
// Show the login controls on the UI thread.
// OnLoginInfoEntered event will return the values entered (username and password).
InvokeOnMainThread(ShowLoginUI);
// Return the login task, the result will be ready when completed (user provides login info and clicks the "Login" button).
return await _loginTaskCompletionSource.Task;
}
// Handle the OnLoginEntered event from the login UI.
// LoginEventArgs contains the username and password that were entered.
private async void LoginEntered(string username, string password)
{
// Make sure the task completion source has all the information needed.
if (_loginTaskCompletionSource == null ||
_loginTaskCompletionSource.Task == null ||
_loginTaskCompletionSource.Task.AsyncState == null)
{
return;
}
try
{
// Get the associated CredentialRequestInfo (will need the URI of the service being accessed).
CredentialRequestInfo requestInfo = (CredentialRequestInfo) _loginTaskCompletionSource.Task.AsyncState;
// Create a token credential using the provided username and password.
TokenCredential userCredentials = await AuthenticationManager.Current.GenerateCredentialAsync
(requestInfo.ServiceUri,
username,
password,
requestInfo.GenerateTokenOptions);
// Set the result on the task completion source.
_loginTaskCompletionSource.TrySetResult(userCredentials);
}
catch (Exception ex)
{
// Unable to create credential, set the exception on the task completion source.
_loginTaskCompletionSource.TrySetException(ex);
}
}
private void ShowLoginUI()
{
// Prompt for the type of convex hull to create.
UIAlertController loginAlert = UIAlertController.Create("Authenticate", "", UIAlertControllerStyle.Alert);
loginAlert.AddTextField(field => field.Placeholder = "Username = user1");
loginAlert.AddTextField(field => field.Placeholder = "Password = user1");
loginAlert.AddAction(UIAlertAction.Create("Log in", UIAlertActionStyle.Default, action => { LoginEntered(loginAlert.TextFields[0].Text, loginAlert.TextFields[1].Text); }));
loginAlert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
// Show the alert.
PresentViewController(loginAlert, true, null);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Initialize();
}
public override void LoadView()
{
// Create the views.
View = new UIView {BackgroundColor = UIColor.White};
_myMapView = new MapView();
_myMapView.TranslatesAutoresizingMaskIntoConstraints = false;
_publicLayerLabel = new UILabel
{
Text = "public layer",
AdjustsFontSizeToFitWidth = true,
TextAlignment = UITextAlignment.Center,
BackgroundColor = UIColor.FromWhiteAlpha(0, .6f),
TextColor = UIColor.White,
Lines = 1,
TranslatesAutoresizingMaskIntoConstraints = false
};
_secureLayerLabel = new UILabel
{
Text = "secure layer",
AdjustsFontSizeToFitWidth = true,
TextAlignment = UITextAlignment.Center,
BackgroundColor = UIColor.FromWhiteAlpha(0, .6f),
TextColor = UIColor.White,
Lines = 1,
TranslatesAutoresizingMaskIntoConstraints = false
};
// Add the views.
View.AddSubviews(_myMapView, _publicLayerLabel, _secureLayerLabel);
// Lay out the views.
NSLayoutConstraint.ActivateConstraints(new[]
{
_publicLayerLabel.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
_publicLayerLabel.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
_publicLayerLabel.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
_publicLayerLabel.HeightAnchor.ConstraintEqualTo(40),
_secureLayerLabel.TopAnchor.ConstraintEqualTo(_publicLayerLabel.BottomAnchor),
_secureLayerLabel.LeadingAnchor.ConstraintEqualTo(_publicLayerLabel.LeadingAnchor),
_secureLayerLabel.TrailingAnchor.ConstraintEqualTo(_publicLayerLabel.TrailingAnchor),
_secureLayerLabel.HeightAnchor.ConstraintEqualTo(40),
_myMapView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
_myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
_myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
_myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor)
});
}
}
}