Exemple #1
0
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            HttpRequestMessageProperty httpRequest = null;

            if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
            {
                httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            }

            if (httpRequest == null)
            {
                httpRequest = new HttpRequestMessageProperty()
                {
                    Method             = "GET",
                    SuppressEntityBody = true
                };
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequest);
            }
            WebHeaderCollection headers = httpRequest.Headers;

            string token = cacheToken ? AppDomainHelper.TokenDictionary.GetToken(oAuth_Url, tenant_Id, client_Id, client_Secret, resource, grant_Type)
                : TokenDictionary.GetNewToken(oAuth_Url, tenant_Id, client_Id, client_Secret, resource, grant_Type).Token;

            //Remove the authorization header if already exists.
            headers.Remove(HttpRequestHeader.Authorization);
            headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
            return(null);
        }
 private static Lazy <TokenDictionary> CreateLazyWrapperOnDefaultAppDomain()
 {
     return(new Lazy <TokenDictionary>(() =>
     {
         var wrapper = new TokenDictionary();
         AppDomain.CurrentDomain.SetData(dicGuid, wrapper);
         return wrapper;
     }));
 }
Exemple #3
0
        private static void Initialize()
        {
            TokenDictionary.WriteLine(AppDomain.CurrentDomain.FriendlyName);
            initializing = true;
            var    host = new mscoree.CorRuntimeHost();
            object defaultAppDomain;

            host.GetDefaultDomain(out defaultAppDomain);
            AppDomain defAppDomain = (AppDomain)defaultAppDomain;

            defAppDomain.DoCallBack(new CrossAppDomainDelegate(InitializeFromWrapper));
            initializing = false;
        }
 private static Lazy <TokenDictionary> CreateLazyWrapperOnOtherDomain()
 {
     return(new Lazy <TokenDictionary>(() =>
     {
         var defAppDomain = AppDomainHelper.DefaultAppDomain;
         TokenDictionary wrapper = (TokenDictionary)defAppDomain.GetData(dicGuid);
         if (wrapper == null)
         {
             defAppDomain.DoCallBack(CreateCallback);
             wrapper = (TokenDictionary)defAppDomain.GetData(dicGuid);
         }
         return wrapper;
     }));
 }
Exemple #5
0
 internal static void InitializeFromWrapper()
 {
     TokenDictionary.WriteLine(AppDomain.CurrentDomain.FriendlyName);
     if (!initialized)
     {
         initThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
         tokenDic     = new MyConcurrentDictionary <string, TokenInfo>();
         TokenDictionary.WriteLine("Dictionary Initialized, threadId:{0}", initThreadId);
         localTimer          = new System.Timers.Timer(10000);
         localTimer.Elapsed += LocalTimer_Elapsed;
         //GC.SuppressFinalize(tokenDic);
         //GC.SuppressFinalize(localTimer);
         initialized = true;
     }
 }
Exemple #6
0
        private static void LocalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            TokenDictionary.WriteLine("Timer, Dic Count: {0}", tokenDic.Count);
            var keys = tokenDic.Keys.ToList();

            foreach (string key in keys)
            {
                TokenInfo ti = tokenDic[key];
                if (!ti.IsValid & !ti.IsWaiting)
                {
                    tokenDic.TryRemove(key, out ti);
                }
            }
            if (tokenDic.Count == 0)
            {
                localTimer.Enabled = false;
                TokenDictionary.WriteLine("Timer is disabled");
                WriteLogMessage("No more valid token available in the dictionary");
            }
        }
        public string GetToken(string oAuth2Url, string tenant_Id, string client_Id, string client_Secret, string resource, string grantType = "client_credentials")
        {
            string key = string.Format("url={0}"
                                       + "&tenant_id={1}"
                                       + "&client_id={2}"
                                       + "&client_secret={3}"
                                       + "&grant_type={4}"
                                       + "&resource={5}",
                                       oAuth2Url,
                                       tenant_Id,
                                       client_Id,
                                       client_Secret,
                                       grantType,
                                       resource);
            TokenInfo ti = TokenDictionary.TheInstance.GetOrCreateTokenInfo(key);

            while (ti.IsWaiting)
            {
                Thread.Sleep(100);
            }
            if (!ti.IsValid)
            {
                ti.IsWaiting = true;
                try
                {
                    var newToken = GetNewToken(oAuth2Url, tenant_Id, client_Id, client_Secret, resource, grantType);
                    var ti1      = AppDomainHelper.LazyWrapper.Value.UpdateToken(newToken, key);
                    //ti.UpdateFrom(newToken);
                }
                finally
                {
                    ti.IsWaiting = false;
                }
            }
            else
            {
                TokenDictionary.WriteLogMessage("Get token from dictionary");
            }
            return(ti.Token);
        }
Exemple #8
0
 public void InitializeInAppDomain()
 {
     TokenDictionary.InitializeFromWrapper();
 }