public async Task LoginUserWithPassword() { IsLoading = true; await RemoveUserCookies(); var result = await _authenticationManager.AuthenticateAsync(Username, Password); if (!result.IsSuccess) { await ResultChecker.SendMessageDialogAsync(result.Error, false); IsLoading = false; return; } WebManager = new WebClient(result.AuthenticationCookieContainer); var userManager = new UserManager(WebManager); // 0 gets us the default user. var userResult = await userManager.GetUserFromProfilePageAsync(0); if (userResult == null) { await ResultChecker.SendMessageDialogAsync("Failed to get user", false); IsLoading = false; return; } try { var newUser = new UserAuth { AvatarLink = userResult.AvatarLink, IsDefaultUser = true, UserName = userResult.Username, CookiePath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, Guid.NewGuid().ToString() + ".cookie") }; await UserAuthDatabase.AddOrUpdateUser(newUser); CookieManager.SaveCookie(result.AuthenticationCookieContainer, newUser.CookiePath); } catch (Exception ex) { await ResultChecker.SendMessageDialogAsync($"Failed to save user: {ex.Message}", false); IsLoading = false; return; } IsLoading = false; IsLoggedIn = true; App.ShellViewModel.PopulateNavItems(); NavigationService.Navigate(typeof(Views.MainPage)); }
public static UserState GetDefaultAuthWebManager() { var user = UserAuthDatabase.GetDefaultUser(); if (user == null) { return new UserState { IsLoggedIn = false, WebManager = new WebClient() } } ; var webManager = CreateAuthWebManager(user); return(new UserState { IsLoggedIn = true, WebManager = webManager, User = user }); } }
public async Task LogoutUser() { IsLoading = true; try { //await _authenticationManager.LogoutAsync(WebManager.AuthenticationCookie); await RemoveUserCookies(); await UserAuthDatabase.RemoveUser(User); IsLoggedIn = false; App.ShellViewModel.PopulateNavItems(); } catch (Exception ex) { await ResultChecker.SendMessageDialogAsync($"Failed to log user out: {ex.Message}", false); } IsLoading = false; }