static IAzureSession CreateInstance(IDataStore dataStore = null)
        {
            string profilePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                Resources.AzureDirectoryName);

            dataStore = dataStore ?? new DiskDataStore();

            var session = new AdalSession
            {
                ClientFactory         = new ClientFactory(),
                AuthenticationFactory = new AuthenticationFactory(),
                DataStore             = dataStore,
                OldProfileFile        = "WindowsAzureProfile.xml",
                OldProfileFileBackup  = "WindowsAzureProfile.xml.bak",
                ProfileDirectory      = profilePath,
                ProfileFile           = "AzureProfile.json",
            };

            var autoSave = InitializeSessionSettings(dataStore, profilePath, ContextAutosaveSettings.AutoSaveSettingsFile);

            session.ARMContextSaveMode  = autoSave.Mode;
            session.ARMProfileDirectory = autoSave.ContextDirectory;
            session.ARMProfileFile      = autoSave.ContextFile;
            session.TokenCacheDirectory = autoSave.CacheDirectory;
            session.TokenCacheFile      = autoSave.CacheFile;
            session.TokenCache          = InitializeTokenCache(dataStore, session.TokenCacheDirectory, session.TokenCacheFile, autoSave.Mode);
            InitializeDataCollection(session);
            session.RegisterComponent(HttpClientOperationsFactory.Name, () => HttpClientOperationsFactory.Create());
            return(session);
        }
示例#2
0
        public ManagedServiceAccessToken(IAzureAccount account, IAzureEnvironment environment, string resourceId, string tenant = "Common")
        {
            if (account == null || string.IsNullOrEmpty(account.Id) || !account.IsPropertySet(AzureAccount.Property.MSILoginUri))
            {
                throw new ArgumentNullException(nameof(account));
            }

            if (string.IsNullOrWhiteSpace(tenant))
            {
                throw new ArgumentNullException(nameof(tenant));
            }

            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            _account    = account;
            _resourceId = GetResource(resourceId, environment);
            var baseUri = _account.GetProperty(AzureAccount.Property.MSILoginUri);
            var builder = new UriBuilder(baseUri);

            builder.Query = string.Format("resource={0}", Uri.EscapeDataString(_resourceId));
            _requestUri   = builder.Uri.ToString();
            _tenant       = tenant;
            IHttpOperationsFactory factory;

            if (!AzureSession.Instance.TryGetComponent(HttpClientOperationsFactory.Name, out factory))
            {
                factory = HttpClientOperationsFactory.Create();
            }

            _tokenGetter = factory.GetHttpOperations <ManagedServiceTokenInfo>().WithHeader("Metadata", new[] { "true" });
        }
        public ManagedServiceAccessToken(IAzureAccount account, IAzureEnvironment environment, string resourceId, string tenant = "Common")
        {
            if (account == null || string.IsNullOrEmpty(account.Id) || !account.IsPropertySet(AzureAccount.Property.MSILoginUri))
            {
                throw new ArgumentNullException(nameof(account));
            }

            if (string.IsNullOrWhiteSpace(tenant))
            {
                throw new ArgumentNullException(nameof(tenant));
            }

            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            _account    = account;
            _resourceId = GetResource(resourceId, environment);
            var idType = GetIdentityType(account);

            foreach (var uri in BuildTokenUri(_account.GetProperty(AzureAccount.Property.MSILoginUri), account, idType, _resourceId))
            {
                RequestUris.Enqueue(uri);
            }

            if (account.IsPropertySet(AzureAccount.Property.MSILoginUriBackup))
            {
                foreach (var uri in BuildTokenUri(_account.GetProperty(AzureAccount.Property.MSILoginUriBackup), account, idType, _resourceId))
                {
                    RequestUris.Enqueue(uri);
                }
            }

            _tenant = tenant;
            IHttpOperationsFactory factory;

            if (!AzureSession.Instance.TryGetComponent(HttpClientOperationsFactory.Name, out factory))
            {
                factory = HttpClientOperationsFactory.Create();
            }

            _tokenGetter = factory.GetHttpOperations <ManagedServiceTokenInfo>(true).WithHeader("Metadata", new[] { "true" });
            if (account.IsPropertySet(AzureAccount.Property.MSILoginSecret))
            {
                _tokenGetter = _tokenGetter.WithHeader("Secret", new[] { account.GetProperty(AzureAccount.Property.MSILoginSecret) });
            }
        }
示例#4
0
        static IAzureSession CreateInstance(IDataStore dataStore = null)
        {
            string profilePath = Path.Combine(
#if NETSTANDARD
                Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
                Resources.AzureDirectoryName);
            string oldProfilePath = Path.Combine(
#endif
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                Resources.OldAzureDirectoryName);

            dataStore = dataStore ?? new DiskDataStore();


            string oldCachePath = Path.Combine(profilePath, "TokenCache.dat");
            string cachePath    = Path.Combine(SharedUtilities.GetUserRootDirectory(), ".IdentityService");
            var    session      = new AdalSession
            {
                ClientFactory         = new ClientFactory(),
                AuthenticationFactory = new AuthenticationFactory(),
                DataStore             = dataStore,
                OldProfileFile        = "WindowsAzureProfile.xml",
                OldProfileFileBackup  = "WindowsAzureProfile.xml.bak",
                ProfileDirectory      = profilePath,
                ProfileFile           = "AzureProfile.json",
            };

            var migrated =
#if !NETSTANDARD
                false;
#else
                MigrateSettings(dataStore, oldProfilePath, profilePath);
#endif
            var autoSave = InitializeSessionSettings(dataStore, cachePath, profilePath, ContextAutosaveSettings.AutoSaveSettingsFile, migrated);
            session.ARMContextSaveMode  = autoSave.Mode;
            session.ARMProfileDirectory = autoSave.ContextDirectory;
            session.ARMProfileFile      = autoSave.ContextFile;
            session.TokenCacheDirectory = autoSave.CacheDirectory;
            session.TokenCacheFile      = autoSave.CacheFile;

            InitializeConfigs(session);
            InitializeDataCollection(session);
            session.RegisterComponent(HttpClientOperationsFactory.Name, () => HttpClientOperationsFactory.Create());
            session.TokenCache = session.TokenCache ?? new AzureTokenCache();
            return(session);
        }