Пример #1
0
        /// <summary>
        /// Create a new Translate command with a given default to language
        /// </summary>
        /// <param Name="defaultToLanguage">The default language the command should be translated to</param>
        public TranslateCommand(string defaultToLanguage, PCamera camera)
        {
            DefaultToLanguage = defaultToLanguage;
            Camera            = camera;

            //Request a _AccessToken from the Azure Bing Translate service
            TranslateToken = new AzureToken(Resources.ClientSecrets.ClientID, Resources.ClientSecrets.ClientSecret, @"http://api.microsofttranslator.com");

            //Create hooks for the translate and language detection services using the translate _AccessToken for authorisation
            TranslateService      = new AzureServiceHelper(@"http://api.microsofttranslator.com/v2/Http.svc/Translate", TranslateToken);
            LanguageDetectService = new AzureServiceHelper(@"http://api.microsofttranslator.com/v2/Http.svc/Detect", TranslateToken);
        }
Пример #2
0
        private async Task ParseAuthorizationCode(string authCode)
        {
            Authenticating = false;
            OnPropertyChanged("Authenticating");
            var resp = await TryGetTokenWithAuthCode(Resource, authCode);

            if (resp.Success)
            {
                var token = new AzureToken(resp.ResponseBody);
                SecureStorage.Current.Add(token.Resource, JsonConvert.SerializeObject(token));
                interactiveLoginTask.SetResult(token);
            }
        }
Пример #3
0
        private async Task <AzureToken> AcquireTokenWithRefresh(string resourceUri)
        {
            foreach (var storedToken in SecureStorage.Current.GetAll())
            {
                var token        = JsonConvert.DeserializeObject <AzureToken>(storedToken.Value);
                var refreshToken = token.RefreshToken;

                var resp = await TryGetTokenWithRefresh(resourceUri, refreshToken);

                if (resp.Success)
                {
                    var res = new AzureToken(resp.ResponseBody);
                    MessagingCenter.Send(new ResultMessage <AzureToken>(res), Constants.IOTC_ACCESS_TOKEN);
                    SecureStorage.Current.Add(token.Resource, JsonConvert.SerializeObject(res));
                    return(res);
                }
            }

            return(await AcquireTokenInteractive(resourceUri));
        }
Пример #4
0
        public async Task AttemptLoginAsync()
        {
            EnableLoginButton = false;
            Dialog            = _locator.Dialog;
            Dialog.StartProgressDialog("The Nest", "Logging in...", false);

            try
            {
                if (CurrentUser.Id == "123")
                {
                    LoginAuthenticator.SaveLogin("USERNAME", "118965");

                    _locator.User = "******";

                    Dialog.ChangeDialogText("The Nest", "Loading...");

                    await _locator.Main.InitializeNewUserAsync();

                    Debug.WriteLine($"\n\n\n\nCredentials:{LoginAuthenticator.GetLogin("USERNAME")}");

                    EnableLoginButton = true;

                    Dialog.DismissProgressDialog();
                }
                else
                {
                    try
                    {
                        _azureTokenTable = App.Client.GetTable <AzureToken>();

                        var userTable = await _azureTokenTable
                                        .Where(user => user.Id == CurrentUser.Id).ToListAsync();

                        if (userTable != null)
                        {
                            Remote = userTable[0];
                            if (Authenticator.VerifyPassword(CurrentUser.Password,
                                                             Remote.HashedPassword, Remote.Salt))
                            {
                                LoginAuthenticator.SaveLogin("USERNAME", Remote.Id);
                                _locator.User = CurrentUser.Id;

                                Dialog.ChangeDialogText("The Nest", "Loading...");

                                await _locator.Main.InitializeNewUserAsync();

                                Dialog.DismissProgressDialog();
                                Debug.WriteLine($"\n\nCredentials:" +
                                                $"{LoginAuthenticator.GetLogin("USERNAME")}");
                            }
                            else
                            {
                                Debug.WriteLine("\n\n\n\nWrong Credentials");
                                Dialog.DismissProgressDialog();
                                await Dialog.StartDialogAsync("Error:",
                                                              "\nIncorrect username or password.\n", true, 0);
                            }
                        }
                        else
                        {
                            Dialog.DismissProgressDialog();
                            await Dialog.StartDialogAsync("Error:",
                                                          "\nIncorrect username or password.\n", true, 0);
                        }
                    }

                    catch (System.Net.WebException internetConnectionEx)
                    {
                        Debug.WriteLine($"\n\n\n{internetConnectionEx.Message}");
                        Dialog.DismissProgressDialog();
                        await Dialog.StartToastAsync("Please check your Internet connection.",
                                                     Android.Widget.ToastLength.Long, 150);
                    }

                    catch (ArgumentOutOfRangeException IncorrectCredentials)
                    {
                        Debug.WriteLine($"\n\n\n{IncorrectCredentials.Message}");
                        Dialog.DismissProgressDialog();
                        await Dialog.StartDialogAsync("Error:",
                                                      "Incorrect username or password.\n", true, 0);
                    }

                    catch (Exception UnkownError)
                    {
                        Debug.WriteLine($"\n\n\n{UnkownError.Message}");
                        Dialog.DismissProgressDialog();
                        await Dialog.StartDialogAsync(null,
                                                      "An error occured while logging in.\nPlease try again.", true, 0);
                    }
                    EnableLoginButton = true;
                }
            }
            catch (ArgumentNullException EmptyCredential)
            {
                Debug.WriteLine($"\n\n\n{EmptyCredential.Message}");
                Dialog.DismissProgressDialog();
                await Dialog.StartDialogAsync("Error:",
                                              "Please input both username and password.", true, 0);

                EnableLoginButton = true;
            }
        }