Пример #1
0
        private HttpClient AuthenticateHttpClient(UnifiAccount account)
        {
            var tokenResponse = account.Authenticate();

            Assert.IsFalse(tokenResponse.IsError);
            Assert.IsNotNull(tokenResponse.AccessToken);
            var httpClient = new HttpClient();

            httpClient.SetBearerToken(tokenResponse.AccessToken);
            httpClient.DefaultRequestHeaders.Remove("Accept");
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata.metadata=minimal");
            return(httpClient);
        }
Пример #2
0
        public void TestRemoteAccountList()
        {
            var unifiAccount      = new UnifiAccount("https://unifiserver.xxx", "unifi_username", "unifi_password");
            var remoteAccountList = new RemoteAccountList();

            remoteAccountList.Add(unifiAccount);
            StringWriter stringWriter  = new StringWriter();
            var          xmlSerializer = new XmlSerializer(typeof(RemoteAccountList));

            xmlSerializer.Serialize(stringWriter, remoteAccountList);
            var serializedAccountList = stringWriter.ToString();

            Assert.AreEqual(-1, serializedAccountList.IndexOf(unifiAccount.Password, StringComparison.Ordinal));
            var roundTrip = (RemoteAccountList)xmlSerializer.Deserialize(new StringReader(serializedAccountList));

            Assert.AreEqual(remoteAccountList.Count, roundTrip.Count);
            Assert.AreEqual(unifiAccount, roundTrip[0]);
        }
Пример #3
0
 private bool TestUnifiAccount(UnifiAccount unifiAccount)
 {
     using (var unifiSession = new UnifiSession(unifiAccount))
     {
         try
         {
             var tokenResponse = unifiAccount.Authenticate();
             if (tokenResponse.IsError)
             {
                 string error = tokenResponse.ErrorDescription ?? tokenResponse.Error;
                 MessageDlg.Show(this, TextUtil.LineSeparate(Resources.EditRemoteAccountDlg_TestUnifiAccount_An_error_occurred_while_trying_to_authenticate_, error));
                 if (tokenResponse.Error == "invalid_scope") // Not L10N
                 {
                     tbxClientScope.Focus();
                 }
                 else if (tokenResponse.Error == "invalid_client") // Not L10N
                 {
                     tbxClientSecret.Focus();
                 }
                 else if (tokenResponse.HttpStatusCode == HttpStatusCode.NotFound)
                 {
                     tbxIdentityServer.Focus();
                 }
                 else
                 {
                     textPassword.Focus();
                 }
                 return(false);
             }
         }
         catch (Exception e)
         {
             MessageDlg.ShowWithException(this, Resources.EditRemoteAccountDlg_TestUnifiAccount_An_error_occurred_while_trying_to_authenticate_, e);
             tbxIdentityServer.Focus();
             return(false);
         }
         bool[] contentsAvailable = new bool[1];
         unifiSession.ContentsAvailable += () =>
         {
             lock (contentsAvailable)
             {
                 contentsAvailable[0] = true;
                 Monitor.Pulse(contentsAvailable);
             }
         };
         using (var longWaitDlg = new LongWaitDlg())
         {
             try
             {
                 longWaitDlg.PerformWork(this, 1000, (ILongWaitBroker broker) =>
                 {
                     while (!broker.IsCanceled)
                     {
                         RemoteServerException remoteServerException;
                         if (unifiSession.AsyncFetchContents(unifiAccount.GetRootUrl(),
                                                             out remoteServerException))
                         {
                             if (remoteServerException != null)
                             {
                                 throw remoteServerException;
                             }
                             break;
                         }
                         lock (contentsAvailable)
                         {
                             while (!contentsAvailable[0] && !broker.IsCanceled)
                             {
                                 Monitor.Wait(contentsAvailable, 10);
                             }
                         }
                     }
                 });
             }
             catch (OperationCanceledException)
             {
                 return(false);
             }
             catch (Exception e)
             {
                 MessageDlg.ShowWithException(this, Resources.EditRemoteAccountDlg_TestUnifiAccount_An_exception_occurred_while_trying_to_fetch_the_directory_listing_, e);
                 textServerURL.Focus();
                 return(false);
             }
         }
     }
     MessageDlg.Show(this, Resources.EditChorusAccountDlg_TestSettings_Settings_are_correct);
     return(true);
 }