Example #1
0
        internal static void Login(bool relogin = false)
        {
            var appSettings       = ConfigurationManager.AppSettings;
            var apiName           = appSettings["ApiName"];
            var apiNameNoSpace    = appSettings["ApiNameNoSpace"];
            var credSaveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), apiNameNoSpace);

            if (!Directory.Exists(credSaveDirectory))
            {
                Directory.CreateDirectory(credSaveDirectory);
            }

            var credLocation = Path.Combine(credSaveDirectory, $"{apiNameNoSpace}.credentials");

            if (!relogin && File.Exists(credLocation))
            {
                var protectedData   = File.ReadAllBytes(credLocation);
                var unprotectedJson = ProtectedData.Unprotect(protectedData, s_aditionalEntropy, DataProtectionScope.CurrentUser);

                var authCreds = JsonConvert.DeserializeObject <BasicAuthCredentials>(Encoding.UTF8.GetString(unprotectedJson));

                SwaggerRegistry.SaveCredentials(authCreds);
                return;
            }

            var creds = CredentialManager.PromptForCredentials(
                captionText: $"Login to {apiName}",
                messageText: $"Please login to {apiName}",
                saveCredential: CredentialSaveOption.Selected);

            if (creds != null)
            {
                var basicAuthCredentials = new BasicAuthCredentials {
                    Username = creds.UserName, Password = creds.Password
                };
                var basicAuthAsJson = JsonConvert.SerializeObject(basicAuthCredentials);

                var protectedJson = ProtectedData.Protect(Encoding.UTF8.GetBytes(basicAuthAsJson), s_aditionalEntropy, DataProtectionScope.CurrentUser);
                File.WriteAllBytes(credLocation, protectedJson);
                SwaggerRegistry.SaveCredentials(basicAuthCredentials);
            }
        }
Example #2
0
 public SwaggerRegistry(SwaggerClient client)
 {
     _client  = client;
     Instance = this;
 }