private async void Initialize() { // Display a map with an imagery basemap MyMapView.Map = new Map(Basemap.CreateImagery()); // 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); // Check for hard-coded user name, password, and domain values if (!string.IsNullOrEmpty(NetworkUsername) && !string.IsNullOrEmpty(NetworkPassword) && !string.IsNullOrEmpty(NetworkDomain)) { // Create a hard-coded network credential ArcGISNetworkCredential hardcodedCredential = new ArcGISNetworkCredential { Credentials = new System.Net.NetworkCredential(NetworkUsername, NetworkPassword, NetworkDomain), ServiceUri = new Uri(SecuredPortalUrl) }; // Add the credential to the AuthenticationManager AuthenticationManager.Current.AddCredential(hardcodedCredential); } // Create the login UI (will display when the user accesses a secured resource) _loginPage = new LoginPage(); // Set up event handlers for when the user completes the login entry or cancels _loginPage.OnLoginInfoEntered += LoginInfoEntered; _loginPage.OnCanceled += LoginCanceled; }
/// <summary> /// Click handler for the login button. Uses the provided info to create a network credential. /// </summary> private void LoginButton_Click(object sender, RoutedEventArgs e) { // If no login information is available from the Task, return if (_loginTaskCompletionSrc == null || _loginTaskCompletionSrc.Task == null || _loginTaskCompletionSrc.Task.AsyncState == null) { return; } // Get the login info (helper class) that was stored with the task var loginInfo = _loginTaskCompletionSrc.Task.AsyncState as LoginInfo; try { // Create a new System.Net.NetworkCredential with the username, password, and domain provided var networkCredential = new NetworkCredential(loginInfo.UserName, loginInfo.Password, loginInfo.Domain); // Create a new ArcGISNetworkCredential with the NetworkCredential and URI of the secured resource var credential = new ArcGISNetworkCredential { Credentials = networkCredential, ServiceUri = loginInfo.ServiceUrl }; // Set the result of the login task with the new ArcGISNetworkCredential _loginTaskCompletionSrc.TrySetResult(credential); } catch (Exception ex) { // Report login exceptions at the bottom of the dialog loginInfo.ErrorMessage = ex.Message; } }
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); // Note: for IWA-secured services, your current system credentials will be used by default and you will only // be challenged for resources to which your system account doesn't have access // Check for hard-coded username, password, and domain values if (!string.IsNullOrEmpty(NetworkUsername) && !string.IsNullOrEmpty(NetworkPassword) && !string.IsNullOrEmpty(NetworkDomain)) { // Create a hard-coded network credential (other than the one that started the app, in other words) ArcGISNetworkCredential hardcodedCredential = new ArcGISNetworkCredential { Credentials = new System.Net.NetworkCredential(NetworkUsername, NetworkPassword, NetworkDomain), ServiceUri = new Uri(SecuredPortalUrl) }; // Add the credential to the AuthenticationManager and report that a non-default credential is being used AuthenticationManager.Current.AddCredential(hardcodedCredential); MessagesTextBlock.Text = "Using credentials for user '" + NetworkUsername + "'"; } }
/// <summary> /// Retrieves all ArcGISRuntime credentials stored in the Credential Locker. /// </summary> public static IEnumerable <Credential> RetrieveAll() { var passwordVault = new PasswordVault(); var credentials = new List <Credential>(); foreach (PasswordCredential passwordCredential in passwordVault.RetrieveAll().Where(pc => pc.Resource.StartsWith(ResourcePrefix))) { Credential credential = null; passwordCredential.RetrievePassword(); string userName = passwordCredential.UserName; string passwordValue = passwordCredential.Password; // value stored as password string serviceUrl = passwordCredential.Resource.Substring(ResourcePrefix.Length); // Create the credential depending on the type if (passwordValue.StartsWith(PasswordPrefix)) { string password = passwordValue.Substring(PasswordPrefix.Length); credential = new ArcGISTokenCredential { ServiceUri = serviceUrl, UserName = userName, Password = password, Token = "dummy" }; // dummy to remove once the token will be refreshed pro actively } else if (passwordValue.StartsWith(OAuthRefreshTokenPrefix)) { string refreshToken = passwordValue.Substring(OAuthRefreshTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, OAuthRefreshToken = refreshToken, Token = "dummy" }; } else if (passwordValue.StartsWith(OAuthAccessTokenPrefix)) { string token = passwordValue.Substring(OAuthAccessTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, Token = token, }; } else if (passwordValue.StartsWith(NetworkCredentialPasswordPrefix)) { string password = passwordValue.Substring(NetworkCredentialPasswordPrefix.Length); credential = new ArcGISNetworkCredential { ServiceUri = serviceUrl, Credentials = new NetworkCredential(userName, password) }; } if (credential != null) { credentials.Add(credential); } } return(credentials); }
/// <summary> /// Prompts the user for a credential (username/password) if access to a secured resource is attempted. /// </summary> /// <param name="info">Information about a secured resource (its URI, for example)</param> /// <returns>A credential to use when attempting access to a secured resource</returns> public async Task <Credential> CreateCredentialAsync(CredentialRequestInfo info) { Credential credential = null; try { // Showing a new window with login UI (username/password) must occur on the UI thread credential = this.Dispatcher.Invoke(new Func <Credential>(() => { Credential cred = null; // Exit if the user clicked "Cancel" in the login window // (otherwise, if the user can't provide credentials for a resource they will continue to be challenged) if (_cancelledLogin) { _cancelledLogin = false; return(null); } // Create a new login window var win = new LoginWindow(); win.Owner = this; // Show the window to get user input (if cancelled, false is returned) _cancelledLogin = (win.ShowDialog() == false); if (!_cancelledLogin) { // Get the credential information provided var username = win.UsernameTextBox.Text; var password = win.PasswordTextBox.Password; var domain = win.DomainTextBox.Text; // Create a new network credential using the user input and the URI of the resource cred = new ArcGISNetworkCredential() { Credentials = new NetworkCredential(username, password, domain), ServiceUri = info.ServiceUri }; } // Return the credential return(cred); }) ); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } // Add the credential to the IdentityManager IdentityManager.Current.AddCredential(credential); // Return the credential return(credential); }
private async void OkButton_Click(object?sender, RoutedEventArgs e) { var info = CredentialRequestInfo; if (info == null || _username == null || _password == null) { return; } if (info.AuthenticationType == AuthenticationType.Token) { if (info.ServiceUri == null) { return; } try { Credential = await AuthenticationManager.Current.GenerateCredentialAsync(info.ServiceUri, _username.Text, ConvertToUnsecureString(_password.SecurePassword), info.GenerateTokenOptions); } catch (System.Exception ex) { CreateCredentialError?.Invoke(this, ex); return; } } else if (info.AuthenticationType == AuthenticationType.NetworkCredential) { Credential = new ArcGISNetworkCredential() { Credentials = new System.Net.NetworkCredential(_username.Text, _password.SecurePassword) }; } else { CreateCredentialError?.Invoke(this, new NotSupportedException("Authentication type not supported")); } if (EnableCredentialCache) { var host = (_serverInfo == null ? info.ServiceUri : _serverInfo.ServerUri) !; if (_rememberCredentialsButton != null && _rememberCredentialsButton.IsChecked.HasValue && _rememberCredentialsButton.IsChecked.Value) { CredentialsCache.SaveCredential(_username.Text, _password.SecurePassword, host); } else { CredentialsCache.DeleteCredential(host); } } if (Credential != null) { Completed?.Invoke(this, Credential); } }
// Prompt the user for a credential if unauthorized access to a secured resource is attempted. public async Task <Credential> CreateCredentialAsync(CredentialRequestInfo info) { Credential credential = null; try { // Dispatch to the UI thread to show the login UI. credential = this.Dispatcher.Invoke(new Func <Credential>(() => { Credential cred = null; // Exit if the user clicked "Cancel" in the login window. // The dialog will continue to be challenge the user if incorrect credentials are entered. if (_canceledLogin) { _canceledLogin = false; return(null); } // Create a new login window. var win = new LoginWindow(); // Show the window to get the user login (if canceled, false is returned). _canceledLogin = (win.ShowDialog() == false); if (!_canceledLogin) { // Get the credential information provided. var username = win.UsernameTextBox.Text; var password = win.PasswordTextBox.Password; var domain = win.DomainTextBox.Text; // Create a new network credential using the user input and the URI of the resource. cred = new ArcGISNetworkCredential() { Credentials = new System.Net.NetworkCredential(username, password, domain), ServiceUri = info.ServiceUri }; } // Return the credential return(cred); })); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } // Add the credential to the AuthenticationManager. AuthenticationManager.Current.AddCredential(credential); // Return the credential. return(credential); }
/// <summary> /// Connects to secure portal instance through Integrated Windows Authentication /// </summary> /// <returns> Authenticated Portal credential </returns> public async Task ConnectToPortalAsync() { this.StatusMessage = "Connecting to Portal ..."; // Set uri for Portal var serviceUri = new Uri(Settings.Default.PortalUri); switch (Settings.Default.AuthenticationType) { // For Portal with IWA authentication enabled // Note that this will only work if you have passthrough authentication enabled // If you open the URL in Internet Explorer, and it prompts for credentials, this will probably not work case "IWA": // Get default credential for the authenticated Windows user var networkCredential = CredentialCache.DefaultCredentials.GetCredential(serviceUri, "Basic"); // Create ArcGIS Network credential ArcGISNetworkCredential arcGisCredential = new ArcGISNetworkCredential { Credentials = networkCredential, ServiceUri = serviceUri, }; // Add the credential to the AuthenticationManager Esri.ArcGISRuntime.Security.AuthenticationManager.Current.AddCredential(arcGisCredential); break; // For Portal with OAuth authentication enabled case "OAuth": this.UpdateAuthenticationManager( serviceUri, Settings.Default.OAuthClientID, Settings.Default.OAuthRedirectUri, Settings.Default.OAuthClientSecret); break; // For Portal with no authentication enabled. // Use this for testing the app or if your data does not require to be secured case "None": default: break; } // Call GetData to download mobile map package await this.GetDataAsync(); }
// Handle the OnLoginEntered event from the login UI, LoginEventArgs contains the username, password, and domain that were entered private void LoginEntered(object sender, LoginEventArgs e) { // Make sure the task completion source has all the information needed. if (_loginTaskCompletionSrc == null || _loginTaskCompletionSrc.Task == null || _loginTaskCompletionSrc.Task.AsyncState == null) { return; } try { // Get the associated CredentialRequestInfo (will need the URI of the service being accessed). CredentialRequestInfo requestInfo = _loginTaskCompletionSrc.Task.AsyncState as CredentialRequestInfo; // Create a new network credential using the values entered by the user. var netCred = new System.Net.NetworkCredential(e.Username, e.Password, e.Domain); // Create a new ArcGIS network credential to hold the network credential and service URI. var arcgisCred = new ArcGISNetworkCredential { Credentials = netCred, ServiceUri = requestInfo.ServiceUri }; // Set the task completion source result with the ArcGIS network credential. // AuthenticationManager is waiting for this result and will add it to its Credentials collection. _loginTaskCompletionSrc.TrySetResult(arcgisCred); } catch (Exception ex) { // Unable to create credential, set the exception on the task completion source. _loginTaskCompletionSrc.TrySetException(ex); } finally { // Get rid of the login controls. _loginUI.Hide(); _loginUI = null; } }
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); // Check for hard-coded user name, password, and domain values if (!string.IsNullOrEmpty(NetworkUsername) && !string.IsNullOrEmpty(NetworkPassword) && !string.IsNullOrEmpty(NetworkDomain)) { // Create a hard-coded network credential ArcGISNetworkCredential hardcodedCredential = new ArcGISNetworkCredential { Credentials = new System.Net.NetworkCredential(NetworkUsername, NetworkPassword, NetworkDomain), ServiceUri = new Uri(SecuredPortalUrl) }; // Add the credential to the AuthenticationManager AuthenticationManager.Current.AddCredential(hardcodedCredential); } }
private void Initialize() { // Note: unlike a WPF app, your current system credentials will NOT be used by default in a UWP app and // you will be (initially) challenged even for resources to which your system account has access. // Once you provide your credentials, you will not be challenged again for them // Check for hard-coded user name, password, and domain values if (!string.IsNullOrEmpty(NetworkUsername) && !string.IsNullOrEmpty(NetworkPassword) && !string.IsNullOrEmpty(NetworkDomain)) { // Create a hard-coded network credential (other than the one that started the app, in other words) ArcGISNetworkCredential hardcodedCredential = new ArcGISNetworkCredential { Credentials = new System.Net.NetworkCredential(NetworkUsername, NetworkPassword, NetworkDomain), ServiceUri = new Uri(SecuredPortalUrl) }; // Add the credential to the AuthenticationManager and report that a non-default credential is being used AuthenticationManager.Current.AddCredential(hardcodedCredential); MessagesTextBlock.Text = "Using credentials for user '" + NetworkUsername + "'"; } }
public static async Task <Credential> CreateCredential(CredentialRequestInfo info) { Credential credential = null; try { credential = new ArcGISNetworkCredential { Credentials = new System.Net.NetworkCredential(ServiceConfig.Username, ServiceConfig.Password), ServiceUri = info.ServiceUri }; } catch (TaskCanceledException) { return(credential); } catch (Exception ex) { Debug.WriteLine(ex.Message); throw; } return(credential); }
// Handler for the OnLoginClicked event defined in the LoginDialogFragment, OnEnterCredentialsEventArgs contains the username, password, and domain the user entered private void LoginClicked(object sender, OnEnterCredentialsEventArgs e) { // If no login information is available from the Task, return. if (_loginTaskCompletionSrc == null || _loginTaskCompletionSrc.Task == null || _loginTaskCompletionSrc.Task.AsyncState == null) { return; } // Get the CredentialRequestInfo object that was stored with the task. var credRequestInfo = _loginTaskCompletionSrc.Task.AsyncState as CredentialRequestInfo; try { // Create a new System.Net.NetworkCredential with the user name, password, and domain provided. var networkCredential = new System.Net.NetworkCredential(e.Username, e.Password, e.Domain); // Create a new ArcGISNetworkCredential with the NetworkCredential and URI of the secured resource. var credential = new ArcGISNetworkCredential { Credentials = networkCredential, ServiceUri = credRequestInfo.ServiceUri }; // Set the result of the login task with the new ArcGISNetworkCredential. _loginTaskCompletionSrc.TrySetResult(credential); } catch (Exception ex) { _loginTaskCompletionSrc.TrySetException(ex); } finally { // Set the task completion source to null to indicate authentication is complete. _loginTaskCompletionSrc = null; } }
// Login button handler - checks entered credentials private async void btnLogin_Click(object sender, RoutedEventArgs e) { if (_loginTCS == null || _loginTCS.Task == null || _loginTCS.Task.AsyncState == null) return; var loginInfo = _loginTCS.Task.AsyncState as LoginInfo; try { Credential credential = null; try { // Showing a new window with login UI (username/password) must occur on the UI thread credential = this.Dispatcher.Invoke(new Func<Credential>(() => { Credential cred = null; var username = loginInfo.UserName; var password = loginInfo.Password; var domain = "AVWORLD"; // Create a new network credential using the user input and the URI of the resource cred = new ArcGISNetworkCredential() { Credentials = new NetworkCredential(username, password, domain), ServiceUri = loginInfo.ServiceUrl }; //} // Return the credential return cred; }) ); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } // Add the credential to the IdentityManager IdentityManager.Current.AddCredential(credential); //var credentials = await IdentityManager.Current.GenerateCredentialAsync(loginInfo.ServiceUrl, // loginInfo.UserName, loginInfo.Password, loginInfo.RequestInfo.GenerateTokenOptions); _loginTCS.TrySetResult(credential); } catch (Exception ex) { loginInfo.ErrorMessage = ex.Message; loginInfo.AttemptCount++; if (loginInfo.AttemptCount >= 3) { _loginTCS.TrySetException(ex); } } }
/// <summary> /// Retrieves all ArcGISRuntime credentials stored in the isolated storage. /// </summary> public static IEnumerable <Credential> RetrieveAll() { var credentials = new List <Credential>(); foreach (var cachedCredential in GetCachedCredentials()) { Credential credential = null; string userName = cachedCredential.UserName; string passwordValue = cachedCredential.Password; // value stored as password string serviceUrl = cachedCredential.Url; // Create the credential depending on the type if (passwordValue.StartsWith(PasswordPrefix)) { string password = passwordValue.Substring(PasswordPrefix.Length); credential = new ArcGISTokenCredential { ServiceUri = serviceUrl, UserName = userName, Password = password, Token = "dummy" }; // dummy to remove once the token will be refreshed pro actively } else if (passwordValue.StartsWith(OAuthRefreshTokenPrefix)) { string refreshToken = passwordValue.Substring(OAuthRefreshTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, OAuthRefreshToken = refreshToken, Token = "dummy" }; } else if (passwordValue.StartsWith(OAuthAccessTokenPrefix)) { string token = passwordValue.Substring(OAuthAccessTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, Token = token, }; } else if (passwordValue.StartsWith(NetworkCredentialPasswordPrefix)) { string password = passwordValue.Substring(NetworkCredentialPasswordPrefix.Length); credential = new ArcGISNetworkCredential { ServiceUri = serviceUrl, Credentials = new NetworkCredential(userName, password) }; } else if (passwordValue.StartsWith(CertificateCredentialPrefix)) { string serial = passwordValue.Substring(CertificateCredentialPrefix.Length); var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); X509Certificate2Collection certificates; try { store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); // Find certificate by serial number certificates = store.Certificates.Find(X509FindType.FindBySerialNumber, serial, true); } catch (Exception) { certificates = null; } finally { store.Close(); } if (certificates != null && certificates.Count > 0) { credential = new CertificateCredential(certificates[0]) { ServiceUri = serviceUrl } } ; } if (credential != null) { credentials.Add(credential); } } return(credentials); }
// Handler for the OnLoginClicked event defined in the LoginDialogFragment // OnEnterCredentialsEventArgs contains the username, password, and domain the user entered private void LoginClicked(object sender, OnEnterCredentialsEventArgs e) { // If no login information is available from the Task, return if (_loginTaskCompletionSrc == null || _loginTaskCompletionSrc.Task == null || _loginTaskCompletionSrc.Task.AsyncState == null) { return; } // Get the CredentialRequestInfo object that was stored with the task var credRequestInfo = _loginTaskCompletionSrc.Task.AsyncState as CredentialRequestInfo; try { // Create a new System.Net.NetworkCredential with the user name, password, and domain provided var networkCredential = new System.Net.NetworkCredential(e.Username, e.Password, e.Domain); // Create a new ArcGISNetworkCredential with the NetworkCredential and URI of the secured resource var credential = new ArcGISNetworkCredential { Credentials = networkCredential, ServiceUri = credRequestInfo.ServiceUri }; // Set the result of the login task with the new ArcGISNetworkCredential _loginTaskCompletionSrc.TrySetResult(credential); } catch (Exception ex) { _loginTaskCompletionSrc.TrySetException(ex); } finally { // Set the task completion source to null to indicate authentication is complete _loginTaskCompletionSrc = null; } }
/// <summary> /// Click handler for the login button. Uses the provided info to create a network credential. /// </summary> private void LoginButton_Click(object sender, RoutedEventArgs e) { // If no login information is available from the Task, return if (_loginTaskCompletionSrc == null || _loginTaskCompletionSrc.Task == null || _loginTaskCompletionSrc.Task.AsyncState == null) return; // Get the login info (helper class) that was stored with the task var loginInfo = _loginTaskCompletionSrc.Task.AsyncState as LoginInfo; try { // Create a new System.Net.NetworkCredential with the username, password, and domain provided var networkCredential = new NetworkCredential(loginInfo.UserName, loginInfo.Password, loginInfo.Domain); // Create a new ArcGISNetworkCredential with the NetworkCredential and URI of the secured resource var credential = new ArcGISNetworkCredential { Credentials = networkCredential, ServiceUri = loginInfo.ServiceUrl }; // Set the result of the login task with the new ArcGISNetworkCredential _loginTaskCompletionSrc.TrySetResult(credential); } catch (Exception ex) { // Report login exceptions at the bottom of the dialog loginInfo.ErrorMessage = ex.Message; } }
// Prompt the user for a credential if unauthorized access to a secured resource is attempted public async Task<Credential> CreateCredentialAsync(CredentialRequestInfo info) { Credential credential = null; try { // Dispatch to the UI thread to show the login UI credential = this.Dispatcher.Invoke(new Func<Credential>(() => { Credential cred = null; // Exit if the user clicked "Cancel" in the login window // (if the user can't provide credentials for a resource they will continue to be challenged) if (_canceledLogin) { _canceledLogin = false; return null; } // Create a new login window var win = new LoginWindow(); win.Owner = this; // Show the window to get user input (if canceled, false is returned) _canceledLogin = (win.ShowDialog() == false); if (!_canceledLogin) { // Get the credential information provided var username = win.UsernameTextBox.Text; var password = win.PasswordTextBox.Password; var domain = win.DomainTextBox.Text; // Create a new network credential using the user input and the URI of the resource cred = new ArcGISNetworkCredential() { Credentials = new System.Net.NetworkCredential(username, password, domain), ServiceUri = info.ServiceUri }; } // Return the credential return cred; })); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } // Add the credential to the AuthenticationManager AuthenticationManager.Current.AddCredential(credential); // Return the credential return credential; }
/// <summary> /// Retrieves all ArcGISRuntime credentials stored in the isolated storage. /// </summary> public static IEnumerable<Credential> RetrieveAll() { var credentials = new List<Credential>(); foreach(var cachedCredential in GetCachedCredentials()) { Credential credential = null; string userName = cachedCredential.UserName; string passwordValue = cachedCredential.Password; // value stored as password string serviceUrl = cachedCredential.Url; // Create the credential depending on the type if (passwordValue.StartsWith(PasswordPrefix)) { string password = passwordValue.Substring(PasswordPrefix.Length); credential = new ArcGISTokenCredential { ServiceUri = serviceUrl, UserName = userName, Password = password, Token = "dummy" }; // dummy to remove once the token will be refreshed pro actively } else if (passwordValue.StartsWith(OAuthRefreshTokenPrefix)) { string refreshToken = passwordValue.Substring(OAuthRefreshTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, OAuthRefreshToken = refreshToken, Token = "dummy" }; } else if (passwordValue.StartsWith(OAuthAccessTokenPrefix)) { string token = passwordValue.Substring(OAuthAccessTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, Token = token, }; } else if (passwordValue.StartsWith(NetworkCredentialPasswordPrefix)) { string password = passwordValue.Substring(NetworkCredentialPasswordPrefix.Length); credential = new ArcGISNetworkCredential { ServiceUri = serviceUrl, Credentials = new NetworkCredential(userName, password) }; } else if (passwordValue.StartsWith(CertificateCredentialPrefix)) { string serial = passwordValue.Substring(CertificateCredentialPrefix.Length); var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); X509Certificate2Collection certificates; try { store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); // Find certificate by serial number certificates = store.Certificates.Find(X509FindType.FindBySerialNumber, serial, true); } catch (Exception) { certificates = null; } finally { store.Close(); } if (certificates != null && certificates.Count > 0) credential = new CertificateCredential(certificates[0]) { ServiceUri = serviceUrl }; } if (credential != null) { credentials.Add(credential); } } return credentials; }
/// <summary> /// Retrieves all ArcGISRuntime credentials stored in the Credential Locker. /// </summary> public static IEnumerable<Credential> RetrieveAll() { var passwordVault = new PasswordVault(); var credentials = new List<Credential>(); foreach (PasswordCredential passwordCredential in passwordVault.RetrieveAll().Where(pc => pc.Resource.StartsWith(ResourcePrefix))) { Credential credential = null; passwordCredential.RetrievePassword(); string userName = passwordCredential.UserName; string passwordValue = passwordCredential.Password; // value stored as password string serviceUrl = passwordCredential.Resource.Substring(ResourcePrefix.Length); // Create the credential depending on the type if (passwordValue.StartsWith(PasswordPrefix)) { string password = passwordValue.Substring(PasswordPrefix.Length); credential = new ArcGISTokenCredential { ServiceUri = serviceUrl, UserName = userName, Password = password, Token = "dummy"}; // dummy to remove once the token will be refreshed pro actively } else if (passwordValue.StartsWith(OAuthRefreshTokenPrefix)) { string refreshToken = passwordValue.Substring(OAuthRefreshTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, OAuthRefreshToken = refreshToken, Token = "dummy" }; } else if (passwordValue.StartsWith(OAuthAccessTokenPrefix)) { string token = passwordValue.Substring(OAuthAccessTokenPrefix.Length); credential = new OAuthTokenCredential { ServiceUri = serviceUrl, UserName = userName, Token = token, }; } else if (passwordValue.StartsWith(NetworkCredentialPasswordPrefix)) { string password = passwordValue.Substring(NetworkCredentialPasswordPrefix.Length); credential = new ArcGISNetworkCredential {ServiceUri = serviceUrl, Credentials = new NetworkCredential(userName, password)}; } if (credential != null) { credentials.Add(credential); } } return credentials; }
// Handle the OnLoginEntered event from the login UI // LoginEventArgs contains the username, password, and domain that were entered private void LoginInfoEntered(object sender, LoginEventArgs e) { // Make sure the task completion source has all the information needed if (_loginTaskCompletionSrc == null || _loginTaskCompletionSrc.Task == null || _loginTaskCompletionSrc.Task.AsyncState == null) { return; } try { // Get the associated CredentialRequestInfo (will need the URI of the service being accessed) CredentialRequestInfo requestInfo = _loginTaskCompletionSrc.Task.AsyncState as CredentialRequestInfo; // Create a new network credential using the values entered by the user var netCred = new System.Net.NetworkCredential(e.Username, e.Password, e.Domain); // Create a new ArcGIS network credential to hold the network credential and service URI var arcgisCred = new ArcGISNetworkCredential { Credentials = netCred, ServiceUri = requestInfo.ServiceUri }; // Set the task completion source result with the ArcGIS network credential // AuthenticationManager is waiting for this result and will add it to its Credentials collection _loginTaskCompletionSrc.TrySetResult(arcgisCred); } catch (Exception ex) { // Unable to create credential, set the exception on the task completion source _loginTaskCompletionSrc.TrySetException(ex); } finally { // Dismiss the login controls Navigation.PopToRootAsync(); } }
// Login button handler - checks entered credentials private async void btnLogin_Click(object sender, RoutedEventArgs e) { if (_loginTCS == null || _loginTCS.Task == null || _loginTCS.Task.AsyncState == null) { return; } var loginInfo = _loginTCS.Task.AsyncState as LoginInfo; try { Credential credential = null; try { // Showing a new window with login UI (username/password) must occur on the UI thread credential = this.Dispatcher.Invoke(new Func <Credential>(() => { Credential cred = null; var username = loginInfo.UserName; var password = loginInfo.Password; var domain = "AVWORLD"; // Create a new network credential using the user input and the URI of the resource cred = new ArcGISNetworkCredential() { Credentials = new NetworkCredential(username, password, domain), ServiceUri = loginInfo.ServiceUrl }; //} // Return the credential return(cred); }) ); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } // Add the credential to the IdentityManager IdentityManager.Current.AddCredential(credential); //var credentials = await IdentityManager.Current.GenerateCredentialAsync(loginInfo.ServiceUrl, // loginInfo.UserName, loginInfo.Password, loginInfo.RequestInfo.GenerateTokenOptions); _loginTCS.TrySetResult(credential); } catch (Exception ex) { loginInfo.ErrorMessage = ex.Message; loginInfo.AttemptCount++; if (loginInfo.AttemptCount >= 3) { _loginTCS.TrySetException(ex); } } }