Exemple #1
0
        public void Authenticate()
        {
            string domain    = this.Tenant;                            // if you want it to automatically use a tenant use "common" - but this can pick the an unintended tenant so it is best to be explicit
            string client_id = "1950a258-227b-4e31-a9cf-717495945fc2"; // Re-use the Azure PowerShell client id, in production code you should create your own client id

            var client_redirect    = new System.Uri("urn:ietf:wg:oauth:2.0:oob");
            var AD_client_settings = REST.Authentication.ActiveDirectoryClientSettings.UseCacheCookiesOrPrompt(client_id, client_redirect);

            // Load the token cache, if one exists.
            string cache_filename = GetTokenCachePath();

            Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache token_cache;

            if (System.IO.File.Exists(cache_filename))
            {
                var bytes = System.IO.File.ReadAllBytes(cache_filename);
                token_cache = new Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache(bytes);
            }
            else
            {
                token_cache = new Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache();
            }

            // Now figure out the token business

            Microsoft.Rest.ServiceClientCredentials creds = null;

            // Get the cached token, if it exists and is not expired.
            //if (token_cache.Count > 0)
            //{
            //    var token_cache_item = token_cache.ReadItems().First();
            //    creds = REST.Authentication.UserTokenProvider.CreateCredentialsFromCache(client_id, token_cache_item.TenantId, token_cache_item.DisplayableId, token_cache).Result;
            //    SaveTokenCache(token_cache, cache_filename);
            //}

            //if (creds == null)
            {
                // Did not find the token in the cache, show popup and save the token
                var sync_context = new System.Threading.SynchronizationContext();
                System.Threading.SynchronizationContext.SetSynchronizationContext(sync_context);
                creds = REST.Authentication.UserTokenProvider.LoginWithPromptAsync(domain, AD_client_settings, token_cache).Result;
                if (token_cache.Count > 0)
                {
                    // If token cache has no items then trying serialize it will fail when deserializing
                    System.IO.File.WriteAllBytes(cache_filename, token_cache.Serialize());
                }
            }



            this.Credentials = creds;
            this.Token       = token_cache.ReadItems().First();
        }
 public override void DeleteItem(TokenCacheItem item)
 {
     base.DeleteItem(item);
     Persist();
 }
        public void DeleteItem(TokenCacheItem item)
#endif
        {
            lock (cacheLock)
            {
                if (item == null)
                {
                    throw new ArgumentNullException("item");
                }

                TokenCacheNotificationArgs args = new TokenCacheNotificationArgs
                {
                    TokenCache = this,
                    Resource = item.Resource,
                    ClientId = item.ClientId,
                    UniqueId = item.UniqueId,
                    DisplayableId = item.DisplayableId
                };

                this.OnBeforeAccess(args);
                this.OnBeforeWrite(args);

                TokenCacheKey toRemoveKey = this.tokenCacheDictionary.Keys.FirstOrDefault(item.Match);
                if (toRemoveKey != null)
                {
                    this.tokenCacheDictionary.Remove(toRemoveKey);
                    Logger.Information(null, "One item removed successfully");
                }
                else
                {
                    Logger.Information(null, "Item not Present in the Cache");
                }

                this.HasStateChanged = true;
                this.OnAfterAccess(args);
            }
        }
        /// <summary>
        /// Deletes an item from the cache.
        /// </summary>
        /// <param name="item">The item to delete from the cache</param>
#if ADAL_NET
        public virtual void DeleteItem(TokenCacheItem item)
 private static bool AreEqual(TokenCacheItem item, TokenCacheKey key)
 {
     return item.Match(key);
 }
        public void DeleteItem(TokenCacheItem item)
#endif
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            TokenCacheNotificationArgs args = new TokenCacheNotificationArgs
                {
                    TokenCache = this,
                    Resource = item.Resource,
                    ClientId = item.ClientId,
                    UniqueId = item.UniqueId,
                    DisplayableId = item.DisplayableId
                };

            this.OnBeforeAccess(args);
            this.OnBeforeWrite(args);

            TokenCacheKey toRemoveKey = this.tokenCacheDictionary.Keys.FirstOrDefault(item.Match);
            if (toRemoveKey != null)
            {
                this.tokenCacheDictionary.Remove(toRemoveKey);
            }

            this.HasStateChanged = true;
            this.OnAfterAccess(args);
        }
 /// <summary>
 /// Instantiates a new <see cref="TokenCacheItemWrapper"/>.
 /// </summary>
 /// <param name="tokenCacheItem">The <see cref="TokenCacheItem"/> to store as the inner cache item.</param>
 public TokenCacheItemWrapper(TokenCacheItem tokenCacheItem)
 {
     this.InnerCacheItem = tokenCacheItem;
 }
Exemple #8
0
        /// <summary>
        /// Deletes an item from the cache.
        /// </summary>
        /// <param name="item">The item to delete from the cache</param>
#if ADAL_NET
        public virtual void DeleteItem(TokenCacheItem item)