private static Task <IdentityManager.Credential> DoSignInInUIThread(IdentityManager.CredentialRequestInfo credentialRequestInfo) { // Create the ChildWindow that contains the SignInDialog var childWindow = new Window { ShowInTaskbar = false, WindowStartupLocation = WindowStartupLocation.CenterOwner, WindowStyle = WindowStyle.ToolWindow, SizeToContent = SizeToContent.WidthAndHeight, ResizeMode = ResizeMode.NoResize, WindowState = WindowState.Normal }; if (Application.Current != null && Application.Current.MainWindow != null) { try { childWindow.Owner = Application.Current.MainWindow; } catch { // May fire an exception when used inside an excel or powerpoint addins } } DependencyProperty titleProperty = Window.TitleProperty; // Create the SignInDialog with the parameters given as arguments var signInDialog = new SignInDialog { Width = 300 }; childWindow.Content = signInDialog; // Bind the Title so the ChildWindow Title is the SignInDialog title (that will be initialized later) var binding = new Binding("Title") { Source = signInDialog }; childWindow.SetBinding(titleProperty, binding); childWindow.Closed += (s, e) => signInDialog.Cancel(); // be sure the SignInDialog is deactivated when closing the childwindow using the X // initialize the task that gets the credential and then close the window var ts = TaskScheduler.FromCurrentSynchronizationContext(); var doSignInTask = signInDialog.GetCredentialAsync(credentialRequestInfo).ContinueWith(task => { childWindow.Close(); return(task.Result); }, ts); // Show the window childWindow.ShowDialog(); return(doSignInTask); }
public LoginInfo(IdentityManager.CredentialRequestInfo cri, string user, string pwd) { RequestInfo = cri; ServiceUrl = new Uri(cri.ServiceUri).GetLeftPart(UriPartial.Path); UserName = user; Password = pwd; ErrorMessage = string.Empty; AttemptCount = 0; }
// Base Challenge method that dispatches to the UI thread if necessary private async Task <IdentityManager.Credential> Challenge(IdentityManager.CredentialRequestInfo cri) { if (Dispatcher == null) { return(await ChallengeUI(cri)); } else { return(await Dispatcher.Invoke(() => ChallengeUI(cri))); } }
/// <summary> /// Gets a credential asynchronously. /// </summary> /// <param name="credentialRequestInfo">The credential request info.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">credentialRequestInfo</exception> public async Task <IdentityManager.Credential> GetCredentialAsync(IdentityManager.CredentialRequestInfo credentialRequestInfo) { if (credentialRequestInfo == null) { throw new ArgumentNullException("credentialRequestInfo"); } Cancel(); // cancel previous task CredentialRequestInfo = credentialRequestInfo; Tcs = new TaskCompletionSource <IdentityManager.Credential>(); using (credentialRequestInfo.CancellationToken.Register(Cancel, true)) { return(await Tcs.Task); } }
// Challenge method that checks for service access with known credentials //private async Task<IdentityManager.Credential> Challenge_KnownCredentials(IdentityManager.CredentialRequestInfo cri) //{ // try // { // // Obtain credenitals from a secure source // string username = "******"; // string password = (cri.ServiceUri.Contains("USA_secure_user1")) ? "user1" : "pass.word1"; // return await IdentityManager.Current.GenerateCredentialAsync(cri.ServiceUri, username, password, cri.GenerateTokenOptions); // } // catch (Exception ex) // { // MessageBox.Show("Acces to " + cri.ServiceUri + " denied. " + ex.Message, "Credential Error"); // } // return await Task.FromResult<IdentityManager.Credential>(null); //} // Challenge method that prompts for username / password private async Task <IdentityManager.Credential> ChallengeUI(IdentityManager.CredentialRequestInfo cri) { try { string username = "******"; string password = (cri.ServiceUri.Contains("USA_secure_user1")) ? "user1" : "pass.word1"; loginPanel.DataContext = new LoginInfo(cri, username, password); _loginTCS = new TaskCompletionSource <IdentityManager.Credential>(loginPanel.DataContext); loginPanel.Visibility = Visibility.Visible; return(await _loginTCS.Task); } finally { loginPanel.Visibility = Visibility.Collapsed; } }
/// <summary> /// Static challenge method leaveraging the SignInDialog in a child window. /// </summary> public static Task <IdentityManager.Credential> DoSignIn(IdentityManager.CredentialRequestInfo credentialRequestInfo) { Dispatcher d = Application.Current == null ? null : Application.Current.Dispatcher; Task <IdentityManager.Credential> doSignInTask; if (d != null && !d.CheckAccess()) { //Ensure we are showing up the SignInDialog on the UI thread var tcs = new TaskCompletionSource <IdentityManager.Credential>(); d.BeginInvoke((Action)(async() => { try { IdentityManager.Credential crd = await DoSignInInUIThread(credentialRequestInfo); tcs.TrySetResult(crd); } catch (Exception error) { tcs.TrySetException(error); } })); doSignInTask = tcs.Task; } else { doSignInTask = DoSignInInUIThread(credentialRequestInfo); } return(doSignInTask.ContinueWith(t => { // Flatten the exceptions if (t.Exception != null) { throw t.Exception.Flatten().InnerException; } return t.Result; })); }
// Challenge method should prompt for portal oauth username / password if necessary public static async Task <IdentityManager.Credential> Challenge(IdentityManager.CredentialRequestInfo arg) { // Register Portal Server if necessary var serverInfo = IdentityManager.Current.FindServerInfo(PORTAL_URL); if (serverInfo == null) { serverInfo = new IdentityManager.ServerInfo() { ServerUri = PORTAL_URL, TokenAuthenticationType = IdentityManager.TokenAuthenticationType.OAuthAuthorizationCode, OAuthClientInfo = new IdentityManager.OAuthClientInfo() { ClientId = CLIENT_ID, RedirectUri = REDIRECT_URI } }; IdentityManager.Current.RegisterServer(serverInfo); } // Use portal URL always (we know all layers are owned by arcgis.com) return(await IdentityManager.Current.GenerateCredentialAsync(PORTAL_URL)); }
// Activate IdentityManager but don't accept any challenge. // User must use the 'SignIn' button for getting its own maps. private Task <IdentityManager.Credential> Challenge(IdentityManager.CredentialRequestInfo arg) { return(Task.FromResult <IdentityManager.Credential>(null)); }
public async Task <bool> SignInUsingIdentityManager(string username, string password) { IsSigningIn = true; // if oauth2 required params are set, register the server for oauth2 authentication. if (App.IsOrgOAuth2) { IdentityManager.ServerInfo si = new IdentityManager.ServerInfo(); si.ServerUri = App.PortalUri.Uri.ToString(); si.TokenAuthenticationType = IdentityManager.TokenAuthenticationType.OAuthAuthorizationCode; si.OAuthClientInfo = new IdentityManager.OAuthClientInfo() { ClientId = App.AppServerId, RedirectUri = App.AppRedirectUri }; IdentityManager.Current.RegisterServer(si); //IdentityManager.Current.TokenValidity = 30; ////ToDo: revisist persisting and retreiving the token for OAuth2 //IdentityManager.Credential cr = await RetrieveCredentialAsync(); //if (cr != null) //{ // IdentityManager.Current.AddCredential(cr); // _credential = cr; // IsSigningIn = false; // return true; //} } // if username and password were retrieved try getting the credentials without challenging the user else if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) { try { var credential = await IdentityManager.Current.GenerateCredentialAsync(App.PortalUri.Uri.ToString(), username, password); if (credential != null && !string.IsNullOrEmpty(credential.Token)) { //set the credential _credential = credential; IsSigningIn = false; return(true); } } catch (Exception ex) { IsSigningIn = false; var _ = App.ShowExceptionDialog(ex); return(false); } } // Since credential could not be retrieved, try getting it by challenging the user var credentialRequestInfo = new IdentityManager.CredentialRequestInfo { ServiceUri = App.PortalUri.Uri.ToString(), AuthenticationType = IdentityManager.AuthenticationType.Token, }; try { IdentityManager.Credential credential = await IdentityManager.Current.GetCredentialAsync(credentialRequestInfo, true); if (credential != null && !string.IsNullOrEmpty(credential.Token)) // && credential.Token != Token) { //set the credential _credential = credential; //store credentials using PasswordVault if (!App.IsOrgOAuth2) // && IdentityManager.Current.ChallengeMethodCredentialResults.CredentialSaveOption == Windows.Security.Credentials.UI.CredentialSaveOption.Selected) { new PasswordVault().Add(new PasswordCredential(App.OrganizationUrl, credential.UserName, credential.Password)); } //else // new PasswordVault().Add(new PasswordCredential(App.OrganizationUrl, credential.UserName, credential.Token)); // for OAuth2 store the token instead of the password. IsSigningIn = false; return(true); } //if (credential.Credentials != null) // && credential.Credentials != credentials) //{ // System.Net.ICredentials credentials = credential.Credentials; // hasChanged = true; //} } catch (Exception) { IsSigningIn = false; } IsSigningIn = false; return(false); }
/// <summary> /// Gets a credential asynchronously. /// </summary> /// <param name="credentialRequestInfo">The credential request info.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">credentialRequestInfo</exception> public async Task<IdentityManager.Credential> GetCredentialAsync(IdentityManager.CredentialRequestInfo credentialRequestInfo) { if (credentialRequestInfo == null) throw new ArgumentNullException("credentialRequestInfo"); Cancel(); // cancel previous task CredentialRequestInfo = credentialRequestInfo; Tcs = new TaskCompletionSource<IdentityManager.Credential>(); using (credentialRequestInfo.CancellationToken.Register(Cancel, true)) { return await Tcs.Task; } }