コード例 #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 IAccessTokenInfo GetAccessTokenInfo(HttpControllerContext httpControllerContext)
        {
            if (httpControllerContext == null)
            {
                throw new ArgumentNullException("httpControllerContext");
            }

            string cacheKey = GetCacheKey(httpControllerContext.Request);

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

                if (!cacheItem.isValid())
                {
                    cacheItem.RefreshTokens();
                    WebAPIContextCache.Instance.Put(cacheKey, cacheItem);
                }


                return(cacheItem);
            }
            else
            {
                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
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            if (filterContext.HttpContext.Request.Cookies["CacheKey"] != null)
            {
                string cacheKey = filterContext.HttpContext.Request.Cookies["CacheKey"].Value;

                IAccessTokenInfo cacheItem = WebAPIContextCache.Instance.Get(cacheKey);
                if (cacheItem != null)
                {
                    if (!cacheItem.isValid())
                    {
                        cacheItem.RefreshTokens();
                        WebAPIContextCache.Instance.Put(cacheKey, cacheItem);
                    }
                    return;
                }
                else
                {
                    // Webbservern har startat om och cachen är tömd
                    //throw new Exception("IAccessTokenInfo is not valid");
                }
            }

            // Första anropet, skapa AccessTokenInfo
            IAccessTokenInfo accessTokenInfo = null;

            if (TokenHelper.IsHighTrustApp())
            {
                accessTokenInfo = new OnPremAccessTokenInfo(filterContext.HttpContext.Request);
            }
            else
            {
                accessTokenInfo = new CloudAccessTokenInfo(filterContext.HttpContext.Request);
            }
            filterContext.HttpContext.Request.Cookies.Add(new System.Web.HttpCookie("CacheKey", accessTokenInfo.CacheKey));
            //filterContext.HttpContext.Request.Cookies["CacheKey"].Value = accessTokenInfo.CacheKey;
            filterContext.HttpContext.Response.Cookies["CacheKey"].Value = accessTokenInfo.CacheKey;
            WebAPIContextCache.Instance.Put(accessTokenInfo.CacheKey, accessTokenInfo);

            //responseMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie });

            // filterContext.Result = new RedirectResult(redirectUrl.AbsoluteUri);
            // filterContext.Result = new ViewResult { ViewName = "Error" };
        }