// Click handler for the login button private void LoginButtonClick(object sender, EventArgs e) { try { // Get information for the login var username = _usernameTextbox.Text; var password = _passwordTextbox.Text; var domain = _domainTextbox.Text; // Make sure all required info was entered if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(domain)) { throw new Exception("Please enter a username, password, and domain."); } // Create a new OnEnterCredentialsEventArgs object to store the information entered by the user var credentialsEnteredArgs = new OnEnterCredentialsEventArgs(username, password, domain); // Raise the OnLoginClicked event so the main activity can handle the event and try to authenticate with the credentials OnLoginClicked(this, credentialsEnteredArgs); // Close the dialog this.Dismiss(); } catch (Exception ex) { // Show the exception message (dialog will stay open so user can try again) var alertBuilder = new AlertDialog.Builder(this.Activity); alertBuilder.SetTitle("Error"); alertBuilder.SetMessage(ex.Message); alertBuilder.Show(); } }
// 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; } }