/// <summary> /// Determines if the entered username/password is valid /// for the case where username/password is needed. /// Also saves any brokenrules to the tbRules textblock /// </summary> /// <returns>true if username/password legal</returns> private bool IsCredentialsValid() { string username = tbUsername.Text; if (string.IsNullOrEmpty(username)) { tbRules.Text = ""; // TODO BAD This function has side effects!! SetRealmHintVisibility(!string.IsNullOrEmpty(realm) && hint ? Visibility.Visible : Visibility.Hidden); // TODO BAD This function has side effects!! mainWindow.btnNext.IsEnabled = false; grpRules.Visibility = Visibility.Hidden; return false; } // if username does not contain '@' and realm is given then show realm added to end // right now only make the realm visible if we must show the hint or the username is not focused, // otherwise we may inadvertedly prevent the user from remembering to use a subdomain // however, if the hint is already visible, we may as well keep it visible if the conditions still match if (!String.IsNullOrEmpty(realm) && !username.Contains('@') && (hint || !tbUsername.IsFocused || tbRealm.Visibility == Visibility.Visible)) { username += "@" + realm; // TODO BAD This function has side effects!! SetRealmHintVisibility(Visibility.Visible); } else { // TODO BAD This function has side effects!! SetRealmHintVisibility(Visibility.Hidden); } var brokenRules = IdentityProviderParser.GetRulesBrokenOnUsername(username, realm, hint).ToList(); bool usernameValid = !brokenRules.Any(); tbRules.Text = ""; if (!usernameValid) { tbRules.Text = string.Join("\n", brokenRules); } bool fieldsValid = (!string.IsNullOrEmpty(pbCredPassword.Password) && usernameValid) || IsConnected; return fieldsValid; }
public static async void RegisterWithMobileServices(string provider) { App.CurrentUser.Id = Guid.NewGuid().ToString(); App.CurrentUser.ProviderIdLong = App.MobileServicesUser.UserId; App.CurrentUser.ProviderIdShort = IdentityProviderParser.GetShortProvider(App.CurrentUser.ProviderIdLong); App.CurrentUser.Token = App.MobileServicesUser.MobileServiceAuthenticationToken; App.CurrentUser.IdentityProvider = IdentityProviderConverter.GetProvider(provider); string registrationId = String.Empty; try { Channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); Channel.PushNotificationReceived += NotificationReceived; HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null); registrationId = CryptographicBuffer.EncodeToBase64String(token.Id); CurrentChannel.Id = Guid.NewGuid().ToString(); CurrentChannel.ChannelUri = Channel.Uri; CurrentChannel.DeviceType = Enums.DeviceType.Windows8; CurrentChannel.UserId = CurrentUser.ProviderIdLong; CurrentChannel.RegistrationId = registrationId; } catch (Exception ex) { HandleInsertChannelException(ex); } try { ApplicationDataManager.StoreValue(ApplicationConstants.UserKey, App.CurrentUser); await UsersTable.InsertAsync(App.CurrentUser); await ChannelsTable.InsertAsync(App.CurrentChannel); } catch (Exception ex) { HandleInsertChannelException(ex); } CompleteUserProfile(); RetrieveFriends(); }
/// <summary> /// Search function called when the search box is changed. /// Does a search through all institutions and sorts it /// to best match the search terms /// </summary> private async void Search() { if (downloader.ClosestProviders == null) { await downloader.LoadProviders(useGeodata : true); } if (downloader.ClosestProviders == null) { return; } // flag so only one search is done at a time if (isSearching) { // remember that a new search term got written so its not forgotten isNewSearch = true; return; } // isSearching was false, so current searchString is newest isNewSearch = false; string searchString = tbSearch.Text; isSearching = true; // async to hinder UI from freezing await Task.Run(() => UpdateInstitutions( IdentityProviderParser.SortByQuery( downloader.ClosestProviders, searchString, limit: 100))); lbInstitutions.SelectedIndex = searchString.Length == 0 ? -1 : 0; isSearching = false; // if search text has changed during await then run the newest search string so its not lost if (isNewSearch) { Search(); } }
/// <summary> /// Invoked when the application is launched normally by the end MobileServicesUser. Other entry points /// will be used such as when the application is launched to open a specific file. /// </summary> /// <param name="e">Details about the launch request and process.</param> protected override async void OnLaunched(LaunchActivatedEventArgs e) { #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { this.DebugSettings.EnableFrameRateCounter = true; } #endif timer.Interval = new TimeSpan(0, 0, 0, 1); //timer.Tick += CheckIfAppIsSuspended(); Frame rootFrame = Window.Current.Content as Frame; ApplicationApplicationState = Enums.ApplicationStateType.Active; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); // Set the default language rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } ChatPageViewModel = ServiceLocator.Current.GetInstance <ChatPageViewModel>(); Window.Current.VisibilityChanged += CheckIfAppIsSuspended; //MainPage.RegisterWithMobileServices(); // Ensure the current window is active Window.Current.Activate(); RegisterBackgroundTask(); if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter Object userEntry = ApplicationDataManager.GetValue(ApplicationConstants.UserKey); if (userEntry != null) { try { App.CurrentUser = JsonConvert.DeserializeObject <UserEntity>((String)userEntry); if (App.CurrentUser != null) { try { if (CurrentUser.IdentityProvider == Enums.IdentityProvider.MicrosoftAccount) { App.MobileServicesUser = await App.MobileService.LoginWithMicrosoftAccountAsync(CurrentUser.AccessToken); } else { dynamic jToken = new JObject(); IdentityProviderParser.RetrieveTokenObjectSpecificToIdentityProvider(CurrentUser.IdentityProvider, CurrentUser.AccessToken, CurrentUser.AccessTokenSecret, out jToken); App.MobileServicesUser = await App.MobileService.LoginAsync(CurrentUser.IdentityProvider.ToString(), jToken); } RegisterWithMobileServices(CurrentUser.IdentityProvider.ToString()); rootFrame.Navigate(typeof(ChatPage), e.Arguments); return; } catch (Exception ex) { ex.ToString(); } } } catch (Exception ex) { ex.ToString(); } } rootFrame.Navigate(typeof(LoginPage), e.Arguments); } }
public async static void RegisterWithMobileServices(string provider) { try { App.CurrentUser.Id = Guid.NewGuid().ToString(); App.CurrentUser.ProviderIdLong = App.MobileServicesUser.UserId; App.CurrentUser.ProviderIdShort = IdentityProviderParser.GetShortProvider(App.CurrentUser.ProviderIdLong); App.CurrentUser.Token = App.MobileServicesUser.MobileServiceAuthenticationToken; App.CurrentUser.IdentityProvider = IdentityProviderConverter.GetProvider(provider); App.Channel = HttpNotificationChannel.Find("MyPushChannel"); if (App.Channel == null) { App.Channel = new HttpNotificationChannel("MyPushChannel"); App.Channel.Open(); } if (App.Channel.ConnectionStatus == ChannelConnectionStatus.Disconnected) { App.Channel.Open(); } if (!App.Channel.IsShellToastBound) { App.Channel.BindToShellToast(); } if (!App.Channel.IsShellToastBound) { App.Channel.BindToShellTile(); } App.Channel.ShellToastNotificationReceived += ToastReceived; App.Channel.ChannelUriUpdated += ChannelUriUpdated; App.Channel.ConnectionStatusChanged += ConnectionStatusChanged; } catch (Exception ex) { HandleInsertChannelException(ex); } try { CurrentChannel.Id = Guid.NewGuid().ToString(); CurrentChannel.DeviceType = Enums.DeviceType.WindowsPhone8; CurrentChannel.UserId = CurrentUser.ProviderIdLong; CurrentChannel.RegistrationId = String.Empty; } catch (Exception ex) { ex.ToString(); } try { ApplicationDataManager.StoreValue(ApplicationConstants.UserKey, App.CurrentUser); await UsersTable.InsertAsync(App.CurrentUser); } catch (Exception ex) { ex.ToString(); } try { if (App.Channel != null && App.Channel.ChannelUri != null) { CurrentChannel.ChannelUri = App.Channel.ChannelUri.AbsoluteUri; } else { int x = 0; x++; } await ChannelsTable.InsertAsync(App.CurrentChannel); } catch (Exception ex) { ex.ToString(); } try { UserEntityProfileCompletion result = await App.MobileService.InvokeApiAsync <UserEntityProfileCompletion>(ApplicationConstants.COMPLETE_USER_PROFILE_METHOD, HttpMethod.Get, null); if (result != null) { App.CurrentUser.Name = result.Name; App.CurrentUser.Picture = result.Picture; App.CurrentUser.AccessToken = result.AccessToken; App.CurrentUser.AccessTokenSecret = result.AccessTokenSecret; ApplicationDataManager.StoreValue(ApplicationConstants.UserKey, App.CurrentUser); } } catch (Exception ex) { ex.ToString(); } try { Dictionary <String, String> query = new Dictionary <string, string>(); query.Add("userId", CurrentUser.ProviderIdLong); List <Friend> friends = await App.MobileService.InvokeApiAsync <List <Friend> >(ApplicationConstants.GET_USERS_METHOD, HttpMethod.Get, query); if (friends != null) { ChatPageViewModel chatPageViewModel = ServiceLocator.Current.GetInstance <ChatPageViewModel>(); chatPageViewModel.FriendsViewModel = new ObservableCollection <Friend>(friends); chatPageViewModel.SelectedFriend = friends[0]; } } catch (Exception ex) { ex.ToString(); } }