private async Task <T> ExecuteCommand <T>(Func <T> command) { var retry = false; var count = 0; T result = default(T); do { count++; try { result = await Task <T> .Factory.StartNew(() => command.Invoke()); } catch (Exception e) { if (ClientType == ClientType.User && e.InnerException != null && e.InnerException is DataServiceClientException ie && (ie.StatusCode == 401 || ie.StatusCode == 403) && count < 5) { retry = true; var c = CredentialPicker.PromptForCredential("test", "test"); if (c != null) { (_ctx as DataServiceContext).Credentials = c; } } else { throw; } }
private async void ToggleSwitch_UseHttpAuth_Toggled(object sender, RoutedEventArgs e) { if (ToggleSwitch_UseHttpAuth.IsOn) { var options = new CredentialPickerOptions() { TargetName = resourceLoader.GetString("Prompt_HttpAuth_Title"), Caption = resourceLoader.GetString("Prompt_HttpAuth_Title"), Message = resourceLoader.GetString("Prompt_HttpAuth_Message"), CredentialSaveOption = CredentialSaveOption.Hidden, AuthenticationProtocol = AuthenticationProtocol.Basic }; var result = await CredentialPicker.PickAsync(options); if (string.IsNullOrEmpty(result.CredentialUserName) || string.IsNullOrWhiteSpace(result.CredentialUserName)) { ToggleSwitch_UseHttpAuth.IsOn = false; return; } else { UseHttpAuth = true; HttpBasicAuthCredential = new HttpBasicAuthCredential(result.CredentialUserName, result.CredentialPassword); } } else { UseHttpAuth = false; HttpBasicAuthCredential = new HttpBasicAuthCredential(); } }
private async void Launch_Click(object sender, RoutedEventArgs e) { if ((Target.Text != "") && (Message.Text != "") && (Caption.Text != "")) { var credPickerResults = await CredentialPicker.PickAsync(Target.Text, Message.Text, Caption.Text); SetResult(credPickerResults); } }
private async Task <CredentialPickerResults> AskCredential(bool retry) { var options = new CredentialPickerOptions() { AuthenticationProtocol = AuthenticationProtocol.Basic, CredentialSaveOption = CredentialSaveOption.Selected, CallerSavesCredential = true, Caption = retry ? resourceLoader.GetString("CredentialPickerRetryCaption") : resourceLoader.GetString("CredentialPickerCaption"), Message = retry ? resourceLoader.GetString("CredentialPickerRetryMessage") : resourceLoader.GetString("CredentialPickerMessage"), TargetName = "." }; return(await CredentialPicker.PickAsync(options)); }
private async Task <CredentialResult> PromptForCredentials(String resource) { CredentialResult credential = null; CredentialPickerOptions options = new CredentialPickerOptions { Message = "Your credentials will be used to connect to JabbR.", Caption = "Please enter your JabbR username and password.", CredentialSaveOption = CredentialSaveOption.Unselected, AuthenticationProtocol = 0, TargetName = resource, }; var result = await CredentialPicker.PickAsync(options); if (result.ErrorCode == 0) { credential = new CredentialResult(result.CredentialUserName, result.CredentialPassword); } return(credential); }
private async void Launch_Click(object sender, RoutedEventArgs e) { if ((Target.Text == "") || (Message.Text == "") || (Caption.Text == "")) { return; } CredentialPickerOptions credPickerOptions = new CredentialPickerOptions(); credPickerOptions.Message = Message.Text; credPickerOptions.Caption = Caption.Text; credPickerOptions.TargetName = Target.Text; credPickerOptions.AlwaysDisplayDialog = (AlwaysShowDialog.IsChecked == true); if (Protocol.SelectedItem == null) { //default protocol, if no selection credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Negotiate; } else { string selectedProtocol = ((ComboBoxItem)Protocol.SelectedItem).Content.ToString(); if (selectedProtocol.Equals("Kerberos", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Kerberos; } else if (selectedProtocol.Equals("Negotiate", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Negotiate; } else if (selectedProtocol.Equals("NTLM", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Ntlm; } else if (selectedProtocol.Equals("CredSsp", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.CredSsp; } else if (selectedProtocol.Equals("Basic", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Basic; } else if (selectedProtocol.Equals("Digest", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Digest; } else if (selectedProtocol.Equals("Custom", StringComparison.CurrentCultureIgnoreCase)) { if (CustomProtocol.Text != null && CustomProtocol.Text != String.Empty) { credPickerOptions.AuthenticationProtocol = AuthenticationProtocol.Custom; credPickerOptions.CustomAuthenticationProtocol = CustomProtocol.Text; } else { rootPage.NotifyUser("Please enter a custom protocol", NotifyType.ErrorMessage); } } } if (CheckboxState.SelectedItem != null) { string checkboxState = ((ComboBoxItem)CheckboxState.SelectedItem).Content.ToString(); if (checkboxState.Equals("Hidden", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.CredentialSaveOption = CredentialSaveOption.Hidden; } else if (checkboxState.Equals("Selected", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.CredentialSaveOption = CredentialSaveOption.Selected; } else if (checkboxState.Equals("Unselected", StringComparison.CurrentCultureIgnoreCase)) { credPickerOptions.CredentialSaveOption = CredentialSaveOption.Unselected; } } var credPickerResults = await CredentialPicker.PickAsync(credPickerOptions); SetResult(credPickerResults); }
private async void Page_Loaded(object sender, RoutedEventArgs e) { (Application.Current as App).context = context; ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; if (settings.Values["LoginSaved"] == null) { settings.Values["Username"] = ""; settings.Values["Password"] = ""; settings.Values["LoginSaved"] = false; } if (await context.AuthenticationIsRequiredAsync()) { while (!loginSuccess) { if ((bool)ApplicationData.Current.LocalSettings.Values["LoginSaved"]) { context.AuthenticationCredentials = new Credentials(settings.Values["Username"].ToString(), settings.Values["Password"].ToString()); loginSuccess = await context.ValidateCredentialsAsync(); if (!loginSuccess) { settings.Values["LoginSaved"] = false; } } else { CredentialPickerResults cpr = await CredentialPicker.PickAsync(new CredentialPickerOptions() { AuthenticationProtocol = AuthenticationProtocol.Basic, Caption = "iMessage Bridge Login", Message = "Authentication is required.", CredentialSaveOption = CredentialSaveOption.Unselected, TargetName = "." }); if (cpr.Credential != null) { if (cpr.CredentialSaved) { settings.Values["LoginSaved"] = true; settings.Values["Username"] = cpr.CredentialUserName; settings.Values["Password"] = cpr.CredentialPassword; } context.AuthenticationCredentials = new Credentials(cpr.CredentialUserName, cpr.CredentialPassword); loginSuccess = await context.ValidateCredentialsAsync(); if (!loginSuccess) { settings.Values["LoginSaved"] = false; } } else { Application.Current.Exit(); } } } } await context.InitAsync(); foreach (KeyValuePair <int, Recipient> r in context.Recipients) { recipientsListBox.Items.Add(r.Value); } foreach (KeyValuePair <int, Conversation> c in context.Conversations) { conversationsListBox.Items.Add(c.Value); } context.StreamUpdate += async(s, ev) => { if (ev.Error == null) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { streamEventsListBox.Items.Add(ev.EventType + " | " + ev.ObjectType + " | " + ev.Object); switch (ev.ObjectType) { case ObjectType.Recipient: recipientsListBox.Items.Clear(); foreach (KeyValuePair <int, Recipient> r in context.Recipients) { recipientsListBox.Items.Add(r.Value); } break; case ObjectType.Conversation: conversationsListBox.Items.Clear(); foreach (KeyValuePair <int, Conversation> c in context.Conversations) { conversationsListBox.Items.Add(c.Value); if (currentConversation != null) { if (currentConversation.Id == c.Value.Id) { currentConversation = c.Value; conversationMessagesListBox.Items.Clear(); foreach (Message m in currentConversation.Messages) { conversationMessagesListBox.Items.Add(m); } } } } break; } if (ev.EventType == EventType.Add && ev.ObjectType == ObjectType.Message) { var message = ev.Object as Message; if (!message.FromMe) { string pictureFileName = Path.GetTempFileName(); if (message.From.HasPicture) { using (IInputStream iis = await context.DownloadRecipientPictureAsync(message.From.Id)) using (FileStream fs = File.Create(pictureFileName)) iis.AsStreamForRead().CopyTo(fs); } ToastNotifier tn = ToastNotificationManager.CreateToastNotifier(); XmlDocument nx = new XmlDocument(); nx.LoadXml(string.Format(@"<toast> <visual> <binding template=""ToastGeneric""> <text hint-maxLines=""1"">{0}</text> <text>{1}</text> <image placement=""appLogoOverride"" hint-crop=""circle"" src=""{2}""/> </binding> </visual> <actions> <input id=""replyTextBox"" type=""text"" placeHolderContent=""Reply...""/> <action content=""Send"" arguments=""address={3}&sms={4}""/> </actions> </toast>", message.From.Name, message.Text, message.From.HasPicture ? pictureFileName : "", message.From.Address, message.ServiceType == ServiceType.SMS ? "1" : "0")); ToastNotification n = new ToastNotification(nx); tn.Show(n); } } }); } else if (ev.Error.Message.ToLower().Contains("closed")) // Reconnect when the stream closes unexpectedly. { await context.StartStreamAsync(); } else { throw ev.Error; } }; }