예제 #1
0
        /// <summary>
        /// Creates a ClientContext token for the incoming WebAPI request. This is done by
        /// - looking up the servicesToken
        /// - extracting the cacheKey
        /// - get the AccessToken from cache. If the AccessToken is expired a new one is requested using the refresh token
        /// - creation of a ClientContext object based on the AccessToken
        /// </summary>
        /// <param name="httpControllerContext">Information about the HTTP request that reached the WebAPI controller</param>
        /// <returns>A valid ClientContext object</returns>
        public static ClientContext GetClientContext(HttpControllerContext httpControllerContext)
        {
            if (httpControllerContext == null)
            {
                throw new ArgumentNullException("httpControllerContext");
            }

            string cacheKey = GetCacheKeyValue(httpControllerContext);

            if (!String.IsNullOrEmpty(cacheKey))
            {
                WebAPIContexCacheItem cacheItem = WebAPIContextCache.Instance.Get(cacheKey);

                //request a new access token from ACS whenever our current access token will expire in less than 1 hour
                if (cacheItem.AccessToken.ExpiresOn < (DateTime.Now.AddHours(-1)))
                {
                    Uri targetUri = new Uri(cacheItem.SharePointServiceContext.HostWebUrl);
                    OAuth2AccessTokenResponse accessToken = TokenHelper.GetAccessToken(cacheItem.RefreshToken, TokenHelper.SharePointPrincipal, targetUri.Authority, TokenHelper.GetRealmFromTargetUrl(targetUri));
                    cacheItem.AccessToken = accessToken;
                    //update the cache
                    WebAPIContextCache.Instance.Put(cacheKey, cacheItem);
                    Log.Info(CoreResources.Services_TokenRefreshed, cacheKey, cacheItem.SharePointServiceContext.HostWebUrl);
                }

                return(TokenHelper.GetClientContextWithAccessToken(cacheItem.SharePointServiceContext.HostWebUrl, cacheItem.AccessToken.AccessToken));
            }
            else
            {
                Log.Warning(Constants.LOGGING_SOURCE, CoreResources.Services_CookieWithCachKeyNotFound);
                throw new Exception("The cookie with the cachekey was not found...nothing can be retrieved from cache, so no clientcontext can be created.");
            }
        }
예제 #2
0
 /// <summary>
 /// Adds an item to the cache. Updates if the item already existed
 /// </summary>
 /// <param name="cacheKey">Key to cache the item</param>
 /// <param name="sharePointServiceContextCacheItem">A <see cref="WebAPIContexCacheItem"/> object</param>
 public void Put(string cacheKey, WebAPIContexCacheItem sharePointServiceContextCacheItem)
 {
     if (!clientContextCache.ContainsKey(cacheKey))
     {
         clientContextCache.Add(cacheKey, sharePointServiceContextCacheItem);
     }
     else
     {
         clientContextCache[cacheKey] = sharePointServiceContextCacheItem;
     }
 }
예제 #3
0
 /// <summary>
 /// Adds an item to the cache. Updates if the item already existed
 /// </summary>
 /// <param name="cacheKey">Key to cache the item</param>
 /// <param name="sharePointServiceContextCacheItem">A <see cref="WebAPIContexCacheItem"/> object</param>
 public void Put(string cacheKey, WebAPIContexCacheItem sharePointServiceContextCacheItem)
 {
     if (!this.clientContextCache.ContainsKey(cacheKey))
     {
         this.clientContextCache.Add(cacheKey, sharePointServiceContextCacheItem);
     }
     else
     {
         this.clientContextCache[cacheKey] = sharePointServiceContextCacheItem;
     }
 }
예제 #4
0
        /// <summary>
        /// Uses the information regarding the requesting app to obtain an access token and caches that using the cachekey.
        /// This method is called from the Register WebAPI service api.
        /// </summary>
        /// <param name="sharePointServiceContext">Object holding information about the requesting SharePoint app</param>
        public static void AddToCache(WebAPIContext sharePointServiceContext)
        {
            if (sharePointServiceContext == null)
            {
                throw new ArgumentNullException("sharePointServiceContext");
            }

            TokenHelper.ClientId          = sharePointServiceContext.ClientId;
            TokenHelper.ClientSecret      = sharePointServiceContext.ClientSecret;
            TokenHelper.HostedAppHostName = sharePointServiceContext.HostedAppHostName;
            SharePointContextToken    sharePointContextToken = TokenHelper.ReadAndValidateContextToken(sharePointServiceContext.Token);
            OAuth2AccessTokenResponse accessToken            = TokenHelper.GetAccessToken(sharePointContextToken, new Uri(sharePointServiceContext.HostWebUrl).Authority);
            WebAPIContexCacheItem     cacheItem = new WebAPIContexCacheItem()
            {
                RefreshToken             = sharePointContextToken.RefreshToken,
                AccessToken              = accessToken,
                SharePointServiceContext = sharePointServiceContext
            };

            WebAPIContextCache.Instance.Put(sharePointServiceContext.CacheKey, cacheItem);
        }
예제 #5
0
        /// <summary>
        /// Uses the information regarding the requesting app to obtain an access token and caches that using the cachekey.
        /// This method is called from the Register WebAPI service api.
        /// </summary>
        /// <param name="sharePointServiceContext">Object holding information about the requesting SharePoint app</param>
        public static void AddToCache(WebAPIContext sharePointServiceContext)
        {
            if (sharePointServiceContext == null)
                throw new ArgumentNullException("sharePointServiceContext");

            TokenHelper.ClientId = sharePointServiceContext.ClientId;
            TokenHelper.ClientSecret = sharePointServiceContext.ClientSecret;
            TokenHelper.HostedAppHostName = sharePointServiceContext.HostedAppHostName;
            SharePointContextToken sharePointContextToken = TokenHelper.ReadAndValidateContextToken(sharePointServiceContext.Token);
            OAuth2AccessTokenResponse accessToken = TokenHelper.GetAccessToken(sharePointContextToken, new Uri(sharePointServiceContext.HostWebUrl).Authority);
            WebAPIContexCacheItem cacheItem = new WebAPIContexCacheItem()
            {
                RefreshToken = sharePointContextToken.RefreshToken,
                AccessToken = accessToken,
                SharePointServiceContext = sharePointServiceContext
            };
            WebAPIContextCache.Instance.Put(sharePointServiceContext.CacheKey, cacheItem);
        }
 /// <summary>
 /// Adds an item to the cache. Updates if the item already existed
 /// </summary>
 /// <param name="cacheKey">Key to cache the item</param>
 /// <param name="sharePointServiceContextCacheItem">A <see cref="WebAPIContexCacheItem"/> object</param>
 public void Put(string cacheKey, WebAPIContexCacheItem sharePointServiceContextCacheItem)
 {
     CacheProvider.Put(cacheKey, sharePointServiceContextCacheItem);
 }