public OnPremAccessTokenInfo(HttpRequestBase request)
        {
            WindowsIdentity logonUserIdentity = request.LogonUserIdentity;

            this.SPHostUrl = WebAPIHelper.GetSPHostUrl(request).ToString();

            string onPremAppOnlyAccessToken =
                TokenHelper.GetS2SAccessTokenWithWindowsIdentity(new Uri(this.SPHostUrl), null);

            string onPremUserAccessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(new Uri(this.SPHostUrl), logonUserIdentity);

            this.RefreshToken = null;
            this.CacheKey     = logonUserIdentity.User.Value;



            // SPAppWebUrl
            string spAppWebUrlString = TokenHelper.EnsureTrailingSlash(request.QueryString[WebAPIHelper.SPAppWebUrlKey]);
            Uri    spAppWebUrl;

            if (!Uri.TryCreate(spAppWebUrlString, UriKind.Absolute, out spAppWebUrl) ||
                !(spAppWebUrl.Scheme == Uri.UriSchemeHttp || spAppWebUrl.Scheme == Uri.UriSchemeHttps))
            {
                spAppWebUrl = null;
            }
            else
            {
                this.SPAppWebUrl = spAppWebUrl.ToString();
            }
            this.AppAccessToken  = onPremAppOnlyAccessToken;
            this.UserAccessToken = onPremUserAccessToken;
            this.Expires         = DateTime.Now.AddDays(1);
        }
        public CloudAccessTokenInfo(HttpRequestBase request)
        {
            try
            {
                var contextToken = TokenHelper.GetContextTokenFromRequest(request);

                SharePointContextToken sharePointContextToken = TokenHelper.ReadAndValidateContextToken(contextToken, request.Url.Authority);
                this.RefreshToken = sharePointContextToken.RefreshToken;

                this.CacheKey = sharePointContextToken.CacheKey;

                this.SPHostUrl = WebAPIHelper.GetSPHostUrl(request).ToString();

                // SPAppWebUrl
                string spAppWebUrlString = TokenHelper.EnsureTrailingSlash(request.QueryString[SharePointContext.SPAppWebUrlKey]);
                Uri    spAppWebUrl;
                if (!Uri.TryCreate(spAppWebUrlString, UriKind.Absolute, out spAppWebUrl) ||
                    !(spAppWebUrl.Scheme == Uri.UriSchemeHttp || spAppWebUrl.Scheme == Uri.UriSchemeHttps))
                {
                    spAppWebUrl = null;
                }
                else
                {
                    this.SPAppWebUrl = spAppWebUrl.ToString();
                }
                var accessToken = TokenHelper.GetAccessToken(this.RefreshToken, TokenHelper.SharePointPrincipal,
                                                             new Uri(this.SPHostUrl).Authority, TokenHelper.GetRealmFromTargetUrl(
                                                                 new Uri(this.SPHostUrl)));

                var userToken = accessToken.AccessToken;

                string appToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(this.SPHostUrl).Authority, TokenHelper.GetRealmFromTargetUrl(
                                                                        new Uri(this.SPHostUrl))).AccessToken;

                this.AppAccessToken  = appToken;
                this.UserAccessToken = userToken;
                this.Expires         = accessToken.ExpiresOn.AddHours(12);
            }
            catch (Exception error)
            {
                //throw new Exception("Redirect user to app");
            }
        }