コード例 #1
0
        /// <summary>
        /// Metoda statyczna ładująca niezbędne dane klienta webowego z pliku typu JSON do usługi.
        /// </summary>
        /// <returns>True, jeśli sekret załadowano pomyślnie.
        ///         False, jeśli sekretu nie udało się załadować.</returns>
        public static bool LoadClietSecretsFromFile()
        {
            ClientSecretsWebClientFileLocation = AppDomain.CurrentDomain.BaseDirectory + "client_secret.json";
            string secrets = null;

            Logs.WriteErrorLog("Laduje sekret klienta z pliku " + ClientSecretsWebClientFileLocation + ".");
            try
            {
                using (StreamReader sr = new StreamReader(ClientSecretsWebClientFileLocation))
                {
                    secrets = sr.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Logs.WriteErrorLog("Wystapil blad podczas czytania pliku \"" + ClientSecretsWebClientFileLocation + "\".");
                Logs.WriteErrorLog(e.Message);
            }

            ClientSecretFile cs = null;

            if (secrets != null)
            {
                cs = JsonConvert.DeserializeObject <ClientSecretFile>(secrets);
            }
            if (cs == null)
            {
                Logs.WriteErrorLog("Wystapil blad podczas serializacji danych pliku " + ClientSecretsWebClientFileLocation);
                return(false);
            }
            ClientId     = cs.Web.ClientId;
            ClientSecret = cs.Web.ClientSecret;
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Metoda sprawdza czy zapisane obecnie dane klienta zgadzają się z danymi w pliku.
        /// </summary>
        /// <returns>True, jeśli wystąpiła zgodność.
        ///          False, jeśli dane były niezgodne i należało je poprawić.</returns>
        public static bool ValidateClientCredentials()
        {
            string secrets = null;

            try
            {
                using (StreamReader sr = new StreamReader(ClientSecretsWebClientFileLocation))
                {
                    secrets = sr.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Logs.WriteErrorLog("Wystapil blad podczas czytania pliku \"" + ClientSecretsWebClientFileLocation + "\". (walidacja nie powiodła się).");
                Logs.WriteErrorLog(e.Message);
            }
            ClientSecretFile cs = null;

            if (secrets != null)
            {
                cs = JsonConvert.DeserializeObject <ClientSecretFile>(secrets);
            }
            if (cs == null)
            {
                Logs.WriteErrorLog("Wystapil blad podczas serializacji danych pliku " + ClientSecretsWebClientFileLocation + " (w trakcie walidacji danych).");
                return(false);
            }
            if (ClientId == cs.Web.ClientId && ClientSecret == cs.Web.ClientSecret)
            {
                return(true);
            }

            ClientId     = cs.Web.ClientId;
            ClientSecret = cs.Web.ClientSecret;
            return(false);
        }