private void StoreTokenInSecureStore(MobileServiceUser user) { var account = new Account(user.UserId); account.Properties.Add(AppUrlConsts.AppUserTokenKey, user.MobileServiceAuthenticationToken); _accountStore.Save(account, AppUrlConsts.AppServiceId); }
public async Task <MobileServiceUser> Authenticate() { var success = false; var message = string.Empty; try { // Sign in with Facebook login using a server-managed flow. MobileServiceClient mobileClient = new MobileServiceClient(Constants.ApplicationURL); user = await mobileClient.LoginAsync(this, MobileServiceAuthenticationProvider.Facebook); if (user != null) { message = string.Format("you are now signed-in as {0}.", user.UserId); } } catch (Exception ex) { message = ex.Message; } // Display the success or failure message. AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetMessage(message); builder.SetTitle("Sign-in result"); builder.Create().Show(); return(user); }
public async Task EnsureLoggedIn() { if (!LoggedIn) { var token = await LoginToken.Load(); if (token != null) { CurrentUser = token.User; // If not authorized, this will log the user out await InternalSyncAsync(); } // If not logged in from cache, show login if (!LoggedIn) { ShowLogin(); } } if (LoggedIn) { MessagingCenter.Send(this, Messages.LoggedIn); } }
public async Task <bool> Authenticate() { string message = string.Empty; var success = false; try { // Sign in with Facebook login using a server-managed flow. if (user == null) { user = await MyXamarinAlliance.App.CharacterService.Client.LoginAsync(MobileServiceAuthenticationProvider.Facebook); if (user != null) { success = true; message = string.Format("You are now signed-in as {0}.", user.UserId); //var guid = await GetDiplomaGuid(); } } } catch (Exception ex) { message = string.Format("Authentication Failed: {0}", ex.Message); } // Display the success or failure message. await new MessageDialog(message, "Sign-in result").ShowAsync(); return(success); }
/// <summary> /// Tests the <see cref="MobileServiceClient.LoginAsync"/> and <see cref="MobileServiceClient.RefreshUserAsync"/> /// functionality of MicrosoftAccount, with <see cref="MobileServiceTable"/> CRUD operations /// </summary> /// <param name="useSingleSignOn"> /// use single sign-on to login the user. /// </param> /// <returns> /// The UserId and MobileServiceAuthentication token obtained after refresh /// </returns> private static async Task <string> TestMicrosoftAccountRefreshUserAsync(bool useSingleSignOn) { await TestCRUDAsync("Public", tableRequiresAuthentication : false, userIsAuthenticated : false); await TestCRUDAsync("Authorized", tableRequiresAuthentication : true, userIsAuthenticated : false); MobileServiceUser user = await LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, uriScheme, useSingleSignOn); // save user.MobileServiceAuthenticationToken value for later use // because RefreshUserAsync() will override user.MobileServiceAuthenticationToken // in single sign-on scenario string authToken = user.MobileServiceAuthenticationToken; await TestCRUDAsync("Public", tableRequiresAuthentication : false, userIsAuthenticated : true); await TestCRUDAsync("Authorized", tableRequiresAuthentication : true, userIsAuthenticated : true); MobileServiceUser refreshedUser = await client.RefreshUserAsync(); string refreshedAuthToken = refreshedUser.MobileServiceAuthenticationToken; Assert.AreEqual(user.UserId, refreshedUser.UserId); Assert.AreNotEqual(authToken, refreshedAuthToken); await TestLogoutAsync(); await TestCRUDAsync("Authorized", tableRequiresAuthentication : true, userIsAuthenticated : false); return(string.Format("User Login and Refresh succeeded. UserId: {0} Token: {1}", refreshedUser.UserId, refreshedUser.MobileServiceAuthenticationToken)); }
private async Task <bool> Authenticate() { var success = false; try { // Sign in with Facebook login using a server-managed flow. user = await client.LoginAsync(this, MobileServiceAuthenticationProvider.Facebook); CreateAndShowDialog(string.Format("you are now logged in - {0}", user.UserId), "Logged in!"); //ServiceHelper serviceHelper = new ServiceHelper(); //String reto = "RetoN" + " + " + "6a47e" + " + https://github.com/DiegoMartinez89/XamarinChampionship/RetoN/"; //String email = "*****@*****.**"; //String AndroidId = Android.Provider.Settings.Secure.GetString(ContentResolver, Android.Provider.Settings.Secure.AndroidId); //Toast.MakeText(this, "Enviando tu registro", ToastLength.Short).Show(); //await serviceHelper.InsertarEntidad(email, reto, AndroidId); //Toast.MakeText(this, "Gracias por registrarte", ToastLength.Long).Show(); success = true; } catch (Exception ex) { CreateAndShowDialog(ex, "Authentication failed"); } return(success); }
private async System.Threading.Tasks.Task AuthenticateTwitter() { while (user == null) { string message; try { user = await App.MobileService .LoginAsync(MobileServiceAuthenticationProvider.Twitter); message = string.Format("Te-ai logat cu succes - {0}", user.UserId); fb.Visibility = Visibility.Collapsed; tw.Visibility = Visibility.Collapsed; App.username = "******"; this.Frame.Navigate(typeof(ItemsPage)); } catch (InvalidOperationException) { message = "Sugereăm să te loghezi."; } var dialog = new MessageDialog(message); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); break; } }
public async Task <MobileServiceUser> GetExpressLoginClient() { if (ExpLogin != null && !string.IsNullOrEmpty(ExpLogin.Password)) { IsBusy = true; var list = await GetExpressLogins(); if (list != null && list.Any()) { var ep = list.FirstOrDefault(x => x.Password == ExpLogin.Password); if (ep != null) { var msu = new MobileServiceUser(ep.UserId); msu.MobileServiceAuthenticationToken = ep.MobileServiceAuthenticationToken; IsBusy = false; return(msu); } else { IsBusy = false; return(null); } } else { IsBusy = false; return(null); } } else { IsBusy = false; return(null); } }
public async Task <MobileServiceUser> LoginAsync(MobileServiceAuthenticationProvider provider) { var view = GetTopViewController(); MobileServiceUser user = await MobileServiceHelper.msInstance.Client.LoginAsync(view, provider); return(user); }
public async Task <bool> LogoutAsync() { bool success = false; try { if (user != null) { foreach (var cookie in NSHttpCookieStorage.SharedStorage.Cookies) { NSHttpCookieStorage.SharedStorage.DeleteCookie(cookie); } await TodoItemManager.DefaultManager.CurrentClient.LogoutAsync(); var logoutAlert = UIAlertController.Create("Authentication", "You are now logged out " + user.UserId, UIAlertControllerStyle.Alert); logoutAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, null)); UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(logoutAlert, true, null); } user = null; success = true; } catch (Exception ex) { var logoutAlert = UIAlertController.Create("Logout failed", ex.Message, UIAlertControllerStyle.Alert); logoutAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, null)); UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(logoutAlert, true, null); } return(success); }
public async Task <bool> AuthenticateAsync() { bool success = false; try { if (user == null) { // The authentication provider could also be Facebook, Twitter, or Microsoft user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync(UIApplication.SharedApplication.KeyWindow.RootViewController, MobileServiceAuthenticationProvider.Google, Constants.URLScheme); if (user != null) { var authAlert = UIAlertController.Create("Authentication", "You are now logged in " + user.UserId, UIAlertControllerStyle.Alert); authAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, null)); UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(authAlert, true, null); } } success = true; } catch (Exception ex) { var authAlert = UIAlertController.Create("Authentication failed", ex.Message, UIAlertControllerStyle.Alert); authAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, null)); UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(authAlert, true, null); } return(success); }
public async Task <bool> Authenticate() { string message = string.Empty; var success = false; try { // Sign in with Facebook login using a server-managed flow. if (user == null) { user = await YodelManager.DefaultManager.CurrentClient .LoginAsync(MobileServiceAuthenticationProvider.Facebook); if (user != null) { Settings.AuthToken = user?.MobileServiceAuthenticationToken ?? string.Empty; Settings.UserId = user?.UserId ?? string.Empty; success = true; message = string.Format("You are now signed-in as {0}.", user.UserId); } } } catch (Exception ex) { message = string.Format("Authentication Failed: {0}", ex.Message); } // Display the success or failure message. await new MessageDialog(message, "Sign-in result").ShowAsync(); return(success); }
public async Task <bool> Authenticate(string provider) { var success = false; try { // Sign in with Facebook login using a server-managed flow. if (user == null) { user = await Locator.Instance.MobileService.LoginAsync( UIApplication.SharedApplication.KeyWindow.RootViewController, ApplicationCapabilities.ConvertString2IdentityProvider(provider)); //if (user != null) //{ // UIAlertView avAlert = new UIAlertView("Authentication", "You are now logged in " + user.UserId, null, "OK", null); // avAlert.Show(); //} } success = true; } catch (Exception ex) { //UIAlertView avAlert = new UIAlertView("Authentication failed", ex.Message, null, "OK", null); //avAlert.Show(); } return(success); }
private AccountResponse AccountFromMobileServiceUser(MobileServiceUser user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } IDictionary <string, string> claims = JwtUtility.GetClaims(user.MobileServiceAuthenticationToken); var account = new AccountResponse(); account.Success = true; account.User = new User { //Id = Guid.Parse(user.UserId), Id = Guid.Parse(claims[JwtClaimNames.Subject]), FirstName = claims[JwtClaimNames.GivenName], LastName = claims[JwtClaimNames.FamilyName], Email = claims[JwtClaimNames.Email], Role = claims[JwtClaimNames.Role], }; account.Token = user.MobileServiceAuthenticationToken; return(account); }
/// <summary> /// Gets the user from vault. /// </summary> /// <param name="passwordVault">The password vault.</param> /// <param name="passwordCredential">The password credential.</param> /// <returns>Task<MobileServiceUser>.</returns> private async static Task <MobileServiceUser> GetUserFromVault(PasswordVault passwordVault, PasswordCredential passwordCredential) { // Create a user from the stored credentials. MobileServiceUser mobileServiceUser = new MobileServiceUser(passwordCredential.UserName); passwordCredential.RetrievePassword(); mobileServiceUser.MobileServiceAuthenticationToken = passwordCredential.Password; // Set the user from the stored credentials. _currentMobileServiceClient.CurrentUser = mobileServiceUser; try { // Try to make a call to verify that the credential has not expired await _currentMobileServiceClient.InvokeApiAsync("Hello", HttpMethod.Get, null); } catch (MobileServiceInvalidOperationException ex) { if (ex.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { // Remove the credential with the expired token. passwordVault.Remove(passwordCredential); mobileServiceUser = null; } } return(mobileServiceUser); }
async public Task <bool> Authenticate(string provider) { var success = false; try { // Sign in with provider specific login using a server-managed flow user = await Locator.Instance.MobileService.LoginAsync(ApplicationCapabilities.ConvertString2IdentityProvider(provider)); if (user != null) { success = true; } else { success = false; } } catch (Exception ex) { //var messageDialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Authentication Failed"); //await messageDialog.ShowAsync(); } return(success); }
public async Task <bool> Authenticate() { var success = false; var message = string.Empty; try { // Sign in with Facebook login using a server-managed flow. if (user == null) { user = await ClientManager.DefaultManager.CurrentClient .LoginAsync(UIApplication.SharedApplication.KeyWindow.RootViewController, MobileServiceAuthenticationProvider.MicrosoftAccount, "imh"); if (user != null) { message = string.Format("You are now signed-in as {0}.", user.UserId); success = true; } } } catch (Exception ex) { message = ex.Message; } // Display the success or failure message. UIAlertView avAlert = new UIAlertView("Sign-in result", message, null, "OK", null); avAlert.Show(); return(success); }
private async System.Threading.Tasks.Task AuthenticateTwitter(MobileServiceAuthenticationProvider provider = MobileServiceAuthenticationProvider.Twitter) { userItems = await usersTable.ToCollectionAsync(); if (user == null) { string message; try { user = await App.MobileService.LoginAsync(provider); message = string.Format("You are now logged in - {0}", user.UserId); var userItem = new userItem { Id = user.UserId, Username = "" }; InsertUserItem(userItem); //dialog = new MessageDialog(message); //dialog.Commands.Add(new UICommand("OK")); //await dialog.ShowAsync(); Frame.Navigate(typeof(MainPage)); } catch (InvalidOperationException) { message = "You must log in. Login Required"; } //extend var dialog = new MessageDialog(message); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); } }
public async Task <bool> Authenticate() { var success = false; var message = string.Empty; try { // Sign in with Facebook login using a server-managed flow. user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync(this, MobileServiceAuthenticationProvider.Google, "url_scheme_of_your_app"); if (user != null) { message = string.Format("you are now signed-in as {0}.", user.UserId); success = true; } } catch (Exception ex) { message = ex.Message; } // Display the success or failure message. AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetMessage(message); builder.SetTitle("Sign-in result"); builder.Create().Show(); return(success); }
private async Task AuthenticateAsync() { var credentials = new AccountModel { username = _loginViewModel.Username, password = _loginViewModel.Password }; try { var jTokenResult = await App.MobileService .InvokeApiAsync("CustomLogin", JToken.FromObject(credentials)); _user = JsonConvert.DeserializeObject <MobileServiceUser>(jTokenResult.ToString()); } catch (InvalidOperationException) { _loginViewModel.ValidationErrorMessage = "Wrong username or password entered."; _loginViewModel.ValidationFailed = true; } if (_user != null) { App.MobileService.CurrentUser = _user; NavigationManager.Navigate(NavigationCommand.Home, Frame); } }
public async Task Logout() { await LoginToken.Delete(); CurrentUser = null; ShowLogin(); }
public async Task <bool> Authenticate() { var success = false; var message = string.Empty; try { // Sign in with Facebook login using a server-managed flow. user = await XamarinAllianceApp.App.MobileService.LoginAsync(this, MobileServiceAuthenticationProvider.Facebook); //https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-users //MobileServiceClient client = new MobileServiceClient("https://xamarinalliancesecurebackend.azurewebsites.net/"); //user = await client.LoginAsync(this, MobileServiceAuthenticationProvider.Facebook); if (user != null) { message = string.Format("you are now signed-in as {0}.", user.UserId); success = true; } } catch (Exception ex) { message = ex.Message; } // Display the success or failure message. AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetMessage(message); builder.SetTitle("Sign-in result"); builder.Create().Show(); return(success); }
public void AuthenticateUser(User user) { CurrentUser = new MobileServiceUser(user.UserId) { MobileServiceAuthenticationToken = user.AuthToken }; }
public async Task <User> RestoreSignInStatus() { var userId = CrossSecureStorage.Current.GetValue("userId", null); var authToken = CrossSecureStorage.Current.GetValue("authToken", null); if (userId == null || authToken == null) { return(null); } var user = new MobileServiceUser(userId); user.MobileServiceAuthenticationToken = authToken; AzureAppService.Current.CurrentUser = user; try { var userContract = await AzureAppService.Current.InvokeApiAsync <UserContract>("User", HttpMethod.Get, null); return(userContract.ToDataModel()); } catch (MobileServiceInvalidOperationException invalidOperationException) { if (invalidOperationException.Response.StatusCode == HttpStatusCode.Unauthorized) { // Remove the credentials. ResetPasswordVault(); AzureAppService.Current.CurrentUser = null; } } return(null); }
public void StoreTokenInSecureStore(MobileServiceUser user) { var account = new Account(user.UserId); account.Properties.Add("token", user.MobileServiceAuthenticationToken); AccountStore.Save(account, Locations.AppName); }
public async Task LogoutAsync() { var vault = new PasswordVault(); var credential = vault.RetrieveAll().FirstOrDefault(); if (credential != null) { var user = new MobileServiceUser(credential.UserName); credential.RetrievePassword(); user.MobileServiceAuthenticationToken = credential.Password; AzureAppService.Current.CurrentUser = user; try { // Try to return an item now to determine if the cached credential has expired. await AzureAppService.Current.LogoutAsync(); } catch (MobileServiceInvalidOperationException) { // We are not interested in any exceptions here } finally { // Make sure vault is cleaned up ResetPasswordVault(); } } await Task.FromResult(0); }
public async Task <bool> Authenticate() { string message = string.Empty; var success = false; try { if (user == null) { user = await MainPageViewModel.Instance.CurrentClient .LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory); if (user != null) { success = true; message = string.Format("You are now signed-in as {0}.", user.UserId); } } } catch (Exception ex) { message = string.Format("Authentication Failed: {0}", ex.Message); } // Display the success or failure message. await new MessageDialog(message, "Sign-in result").ShowAsync(); return(success); }
public async Task <bool> UserLoggedIn() { return(await Task.Run <bool>(() => { var provider = "MicrosoftAccount"; PasswordVault vault = new PasswordVault(); try { // Try to get an existing credential from the vault. var credential = vault.FindAllByResource(provider).FirstOrDefault(); if (credential != null) { credential.RetrievePassword(); App.MobileService.CurrentUser = new MobileServiceUser(credential.UserName) { MobileServiceAuthenticationToken = credential.Password }; this.MobUser = App.MobileService.CurrentUser; return true; } return false; } catch (Exception) { return false; } })); }
public async Task <bool> Authenticate() { var success = false; var message = string.Empty; try { var defaultManagerCurrentClient = FarmLabManager.DefaultManager.CurrentClient; var provider = MobileServiceAuthenticationProvider.Google; // Sign in with Facebook login using a server-managed flow. _user = await defaultManagerCurrentClient.LoginAsync(this, provider, "FarmLab"); if (_user != null) { message = $"you are now signed-in as {_user.UserId}."; success = true; } } catch (Exception ex) { message = ex.Message; } // Display the success or failure message. AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetMessage(message); builder.SetTitle("Sign-in result"); builder.Create().Show(); return(success); }
public void CreateUser() { string id = "qwrdsjjjd8"; MobileServiceUser user = new MobileServiceUser(id); Assert.AreEqual(id, user.UserId); new MobileServiceUser(null); new MobileServiceUser(""); }
private static async Task LoginMicrosoftAsync() { while (m_user == null) { string message; try { m_user = await m_mobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, true); message = string.Format("You are now logged in - {0}", m_user.UserId); } catch (InvalidOperationException) { message = "You must log in. Login Required"; } var dialog = new MessageDialog(message); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); } }